Class SHA1
See RFC 3174.
Unlike MessageDigest, this implementation includes the algorithm used by
sha1dc to detect cryptanalytic collision attacks against SHA-1, such
as the one used by SHAttered. See
sha1collisiondetection for more information.
When detectCollision is true (default), this implementation throws
Sha1CollisionException from any digest
method if a potential collision was detected.
- Since:
- 4.7
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]digest()Finish the digest and return the resulting hash.voidFinish the digest and return the resulting hash.booleanCheck if a collision was detected.static SHA1Create a new context to compute a SHA-1 hash of data.reset()Reset this instance to compute another hash.setDetectCollision(boolean detect) Enable likely collision detection.Finish the digest and return the resulting hash.voidupdate(byte b) Update the digest computation by adding a byte.voidupdate(byte[] in) Update the digest computation by adding bytes to the message.voidupdate(byte[] in, int p, int len) Update the digest computation by adding bytes to the message.
-
Method Details
-
newInstance
Create a new context to compute a SHA-1 hash of data.- Returns:
- a new context to compute a SHA-1 hash of data.
-
setDetectCollision
Enable likely collision detection.Default is
true.May also be set by system property:
-Dorg.eclipse.jgit.util.sha1.detectCollision=true.- Parameters:
detect- a boolean.- Returns:
this
-
update
public void update(byte b) Update the digest computation by adding a byte.- Parameters:
b- a byte.
-
update
public void update(byte[] in) Update the digest computation by adding bytes to the message.- Parameters:
in- input array of bytes.
-
update
public void update(byte[] in, int p, int len) Update the digest computation by adding bytes to the message.- Parameters:
in- input array of bytes.p- offset to start at fromin.len- number of bytes to hash.
-
digest
Finish the digest and return the resulting hash.Once
digest()is called, this instance should be discarded.- Returns:
- the bytes for the resulting hash.
- Throws:
Sha1CollisionException- if a collision was detected and safeHash is false.
-
toObjectId
Finish the digest and return the resulting hash.Once
digest()is called, this instance should be discarded.- Returns:
- the ObjectId for the resulting hash.
- Throws:
Sha1CollisionException- if a collision was detected and safeHash is false.
-
digest
Finish the digest and return the resulting hash.Once
digest()is called, this instance should be discarded.- Parameters:
id- destination to copy the digest to.- Throws:
Sha1CollisionException- if a collision was detected and safeHash is false.
-
hasCollision
public boolean hasCollision()Check if a collision was detected.This method only returns an accurate result after the digest was obtained through
digest(),digest(MutableObjectId)ortoObjectId(), as the hashing function must finish processing to know the final state.- Returns:
trueif a likely collision was detected.
-
reset
-