public class SHA1
extends java.lang.Object
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.
| Modifier and Type | Class and Description |
|---|---|
private static class |
SHA1.State |
| Modifier and Type | Field and Description |
|---|---|
private byte[] |
buffer
Buffer to accumulate partial blocks to 64 byte alignment.
|
private static boolean |
DETECT_COLLISIONS |
private boolean |
detectCollision |
private boolean |
foundCollision |
private SHA1.State |
h |
private SHA1.State |
hIn |
private SHA1.State |
hTmp |
private long |
length
Total number of bytes in the message.
|
private static org.slf4j.Logger |
LOG |
private SHA1.State |
state58 |
private SHA1.State |
state65 |
private int[] |
w |
private int[] |
w2 |
| Modifier | Constructor and Description |
|---|---|
private |
SHA1() |
| Modifier and Type | Method and Description |
|---|---|
private void |
compress() |
private void |
compress(byte[] block,
int p) |
byte[] |
digest()
Finish the digest and return the resulting hash.
|
void |
digest(MutableObjectId id)
Finish the digest and return the resulting hash.
|
private static boolean |
eq(SHA1.State q,
SHA1.State r) |
private void |
finish() |
boolean |
hasCollision()
Check if a collision was detected.
|
private void |
initBlock(byte[] block,
int p) |
static SHA1 |
newInstance()
Create a new context to compute a SHA-1 hash of data.
|
private void |
recompress(int t) |
SHA1 |
reset()
Reset this instance to compute another hash.
|
private static int |
s1(int a,
int b,
int c,
int d,
int w_t) |
private static int |
s2(int a,
int b,
int c,
int d,
int w_t) |
private static int |
s3(int a,
int b,
int c,
int d,
int w_t) |
private static int |
s4(int a,
int b,
int c,
int d,
int w_t) |
SHA1 |
setDetectCollision(boolean detect)
Enable likely collision detection.
|
ObjectId |
toObjectId()
Finish the digest and return the resulting hash.
|
void |
update(byte b)
Update the digest computation by adding a byte.
|
void |
update(byte[] in)
Update the digest computation by adding bytes to the message.
|
void |
update(byte[] in,
int p,
int len)
Update the digest computation by adding bytes to the message.
|
private static org.slf4j.Logger LOG
private static final boolean DETECT_COLLISIONS
private final SHA1.State h
private final int[] w
private final byte[] buffer
private long length
private boolean detectCollision
private boolean foundCollision
private final int[] w2
private final SHA1.State state58
private final SHA1.State state65
private final SHA1.State hIn
private final SHA1.State hTmp
public static SHA1 newInstance()
public SHA1 setDetectCollision(boolean detect)
Default is true.
May also be set by system property:
-Dorg.eclipse.jgit.util.sha1.detectCollision=true.
detect - a boolean.thispublic void update(byte b)
b - a byte.public void update(byte[] in)
in - input array of bytes.public void update(byte[] in,
int p,
int len)
in - input array of bytes.p - offset to start at from in.len - number of bytes to hash.private void compress(byte[] block,
int p)
private void initBlock(byte[] block,
int p)
private void compress()
private void recompress(int t)
private static int s1(int a,
int b,
int c,
int d,
int w_t)
private static int s2(int a,
int b,
int c,
int d,
int w_t)
private static int s3(int a,
int b,
int c,
int d,
int w_t)
private static int s4(int a,
int b,
int c,
int d,
int w_t)
private static boolean eq(SHA1.State q, SHA1.State r)
private void finish()
public byte[] digest()
throws Sha1CollisionException
Once digest() is called, this instance should be discarded.
Sha1CollisionException - if a collision was detected and safeHash is false.public ObjectId toObjectId() throws Sha1CollisionException
Once digest() is called, this instance should be discarded.
Sha1CollisionException - if a collision was detected and safeHash is false.public void digest(MutableObjectId id) throws Sha1CollisionException
Once digest() is called, this instance should be discarded.
id - destination to copy the digest to.Sha1CollisionException - if a collision was detected and safeHash is false.public boolean hasCollision()
This method only returns an accurate result after the digest was obtained
through digest(), digest(MutableObjectId) or
toObjectId(), as the hashing function must finish processing to
know the final state.
true if a likely collision was detected.public SHA1 reset()
this.