forked from opentimestamps/java-opentimestamps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
949 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
out/ | ||
.idea/ | ||
target/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.eternitywall.crypto; | ||
|
||
/** | ||
* interface that a message digest conforms to. | ||
*/ | ||
public interface Digest | ||
{ | ||
/** | ||
* return the algorithm name | ||
* | ||
* @return the algorithm name | ||
*/ | ||
public String getAlgorithmName(); | ||
|
||
/** | ||
* return the size, in bytes, of the digest produced by this message digest. | ||
* | ||
* @return the size, in bytes, of the digest produced by this message digest. | ||
*/ | ||
public int getDigestSize(); | ||
|
||
/** | ||
* update the message digest with a single byte. | ||
* | ||
* @param in the input byte to be entered. | ||
*/ | ||
public void update(byte in); | ||
|
||
/** | ||
* update the message digest with a block of bytes. | ||
* | ||
* @param in the byte array containing the data. | ||
* @param inOff the offset into the byte array where the data starts. | ||
* @param len the length of the data. | ||
*/ | ||
public void update(byte[] in, int inOff, int len); | ||
|
||
/** | ||
* close the digest, producing the final digest value. The doFinal | ||
* call leaves the digest reset. | ||
* | ||
* @param out the array the digest is to be copied into. | ||
* @param outOff the offset into the out array the digest is to start at. | ||
*/ | ||
public int doFinal(byte[] out, int outOff); | ||
|
||
/** | ||
* reset the digest back to it's initial state. | ||
*/ | ||
public void reset(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.eternitywall.crypto; | ||
|
||
|
||
public interface ExtendedDigest | ||
extends Digest | ||
{ | ||
/** | ||
* Return the size in bytes of the internal buffer the digest applies it's compression | ||
* function to. | ||
* | ||
* @return byte length of the digests internal buffer. | ||
*/ | ||
public int getByteLength(); | ||
} |
156 changes: 156 additions & 0 deletions
156
src/main/java/com/eternitywall/crypto/GeneralDigest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package com.eternitywall.crypto; | ||
|
||
/** | ||
* base implementation of MD4 family style digest as outlined in | ||
* "Handbook of Applied Cryptography", pages 344 - 347. | ||
*/ | ||
public abstract class GeneralDigest | ||
implements ExtendedDigest, Memoable | ||
{ | ||
private static final int BYTE_LENGTH = 64; | ||
|
||
private final byte[] xBuf = new byte[4]; | ||
private int xBufOff; | ||
|
||
private long byteCount; | ||
|
||
/** | ||
* Standard constructor | ||
*/ | ||
protected GeneralDigest() | ||
{ | ||
xBufOff = 0; | ||
} | ||
|
||
/** | ||
* Copy constructor. We are using copy constructors in place | ||
* of the Object.clone() interface as this interface is not | ||
* supported by J2ME. | ||
*/ | ||
protected GeneralDigest(GeneralDigest t) | ||
{ | ||
copyIn(t); | ||
} | ||
|
||
protected GeneralDigest(byte[] encodedState) | ||
{ | ||
System.arraycopy(encodedState, 0, xBuf, 0, xBuf.length); | ||
xBufOff = Pack.bigEndianToInt(encodedState, 4); | ||
byteCount = Pack.bigEndianToLong(encodedState, 8); | ||
} | ||
|
||
protected void copyIn(GeneralDigest t) | ||
{ | ||
System.arraycopy(t.xBuf, 0, xBuf, 0, t.xBuf.length); | ||
|
||
xBufOff = t.xBufOff; | ||
byteCount = t.byteCount; | ||
} | ||
|
||
public void update( | ||
byte in) | ||
{ | ||
xBuf[xBufOff++] = in; | ||
|
||
if (xBufOff == xBuf.length) | ||
{ | ||
processWord(xBuf, 0); | ||
xBufOff = 0; | ||
} | ||
|
||
byteCount++; | ||
} | ||
|
||
public void update( | ||
byte[] in, | ||
int inOff, | ||
int len) | ||
{ | ||
len = Math.max(0, len); | ||
|
||
// | ||
// fill the current word | ||
// | ||
int i = 0; | ||
if (xBufOff != 0) | ||
{ | ||
while (i < len) | ||
{ | ||
xBuf[xBufOff++] = in[inOff + i++]; | ||
if (xBufOff == 4) | ||
{ | ||
processWord(xBuf, 0); | ||
xBufOff = 0; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// | ||
// process whole words. | ||
// | ||
int limit = ((len - i) & ~3) + i; | ||
for (; i < limit; i += 4) | ||
{ | ||
processWord(in, inOff + i); | ||
} | ||
|
||
// | ||
// load in the remainder. | ||
// | ||
while (i < len) | ||
{ | ||
xBuf[xBufOff++] = in[inOff + i++]; | ||
} | ||
|
||
byteCount += len; | ||
} | ||
|
||
public void finish() | ||
{ | ||
long bitLength = (byteCount << 3); | ||
|
||
// | ||
// add the pad bytes. | ||
// | ||
update((byte)128); | ||
|
||
while (xBufOff != 0) | ||
{ | ||
update((byte)0); | ||
} | ||
|
||
processLength(bitLength); | ||
|
||
processBlock(); | ||
} | ||
|
||
public void reset() | ||
{ | ||
byteCount = 0; | ||
|
||
xBufOff = 0; | ||
for (int i = 0; i < xBuf.length; i++) | ||
{ | ||
xBuf[i] = 0; | ||
} | ||
} | ||
|
||
protected void populateState(byte[] state) | ||
{ | ||
System.arraycopy(xBuf, 0, state, 0, xBufOff); | ||
Pack.intToBigEndian(xBufOff, state, 4); | ||
Pack.longToBigEndian(byteCount, state, 8); | ||
} | ||
|
||
public int getByteLength() | ||
{ | ||
return BYTE_LENGTH; | ||
} | ||
|
||
protected abstract void processWord(byte[] in, int inOff); | ||
|
||
protected abstract void processLength(long bitLength); | ||
|
||
protected abstract void processBlock(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.eternitywall.crypto; | ||
|
||
|
||
/** | ||
* Interface for Memoable objects. Memoable objects allow the taking of a snapshot of their internal state | ||
* via the copy() method and then reseting the object back to that state later using the reset() method. | ||
*/ | ||
public interface Memoable | ||
{ | ||
/** | ||
* Produce a copy of this object with its configuration and in its current state. | ||
* <p> | ||
* The returned object may be used simply to store the state, or may be used as a similar object | ||
* starting from the copied state. | ||
*/ | ||
Memoable copy(); | ||
|
||
/** | ||
* Restore a copied object state into this object. | ||
* <p> | ||
* Implementations of this method <em>should</em> try to avoid or minimise memory allocation to perform the reset. | ||
* | ||
* @param other an object originally {@link #copy() copied} from an object of the same type as this instance. | ||
* @throws ClassCastException if the provided object is not of the correct type. | ||
* @throws MemoableResetException if the <b>other</b> parameter is in some other way invalid. | ||
*/ | ||
void reset(Memoable other); | ||
} |
Oops, something went wrong.