Skip to content

Commit

Permalink
removing bouncycastle dep
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Mar 6, 2017
1 parent 6718bc7 commit 87dc6a8
Show file tree
Hide file tree
Showing 13 changed files with 949 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
out/
.idea/
target/
.DS_Store
6 changes: 1 addition & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.56</version>
</dependency>
</dependencies>

<build>
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/eternitywall/PendingAttestation.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.eternitywall;

import org.bouncycastle.util.Arrays;

import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;

Expand Down Expand Up @@ -84,6 +82,6 @@ public String toString() {
@Override
public int compareTo(TimeAttestation o) {
PendingAttestation opa = (PendingAttestation) o;
return Arrays.compareUnsigned(this.uri, opa.uri) ;
return Utils.compare(this.uri, opa.uri) ;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/eternitywall/TimeAttestation.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void serializePayload(StreamSerializationContext ctxPayload) {

@Override
public int compareTo(TimeAttestation o) {
int deltaTag = org.bouncycastle.util.Arrays.compareUnsigned(this._TAG(),o._TAG());
int deltaTag = Utils.compare(this._TAG(),o._TAG());
if( deltaTag == 0){
return this.compareTo(o);
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/eternitywall/UnknownAttestation.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.eternitywall;

import org.bouncycastle.util.Arrays;

import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -46,6 +44,6 @@ public String toString() {
@Override
public int compareTo(TimeAttestation o) {
UnknownAttestation ota = (UnknownAttestation) o;
return Arrays.compareUnsigned(this.payload, ota.payload) ;
return Utils.compare(this.payload, ota.payload) ;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/eternitywall/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,15 @@ public static byte[] hexToBytes(String hexString) {
}
return data;
}

public static int compare(byte[] left, byte[] right) {
for (int i = 0, j = 0; i < left.length && j < right.length; i++, j++) {
int a = (left[i] & 0xff);
int b = (right[j] & 0xff);
if (a != b) {
return a - b;
}
}
return left.length - right.length;
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/eternitywall/crypto/Digest.java
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();
}
14 changes: 14 additions & 0 deletions src/main/java/com/eternitywall/crypto/ExtendedDigest.java
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 src/main/java/com/eternitywall/crypto/GeneralDigest.java
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();
}
28 changes: 28 additions & 0 deletions src/main/java/com/eternitywall/crypto/Memoable.java
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);
}
Loading

0 comments on commit 87dc6a8

Please sign in to comment.