1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| public class myDigest implements Digest {
private static final int DIGEST_LENGTH = 2;
private int[] X = new int[4];
private int xOff;
private final byte[] xBuf = new byte[1];
private int xBufOff;
private long byteCount;
/**
* Standard constructor
*/
public myDigest()
{
reset();
}
/**
* return the algorithm name
*
* @return the algorithm name
*/
public String getAlgorithmName() {
String algName = "algTest";
return algName;
}
/**
* 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() {
return DIGEST_LENGTH;
}
/**
* update the message digest with a single byte.
*
* @param in the input byte to be entered.
*/
public void update(byte in) {
xBuf[xBufOff++] = in;
if (xBufOff == xBuf.length)
{
processWord(xBuf, 0);
xBufOff = 0;
}
byteCount++;
}
private void processWord(byte[] in, int inOff) {
// Note: Inlined for performance
// X[xOff] = Pack.bigEndianToInt(in, inOff);
int n = in[ inOff] << 8;
n |= (in[++inOff] & 0xff);
X[xOff] = n;
if (++xOff == 1)
{
processBlock();
}
}
private void processBlock() {
}
/**
* 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) {
if(len > DIGEST_LENGTH) {
try {
throw new Exception("Message too long");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 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) {
if(out.length > DIGEST_LENGTH) {
try {
throw new Exception("Message too long");
} catch (Exception e) {
e.printStackTrace();
}
}
return DIGEST_LENGTH;
}
/**
* reset the digest back to it's initial state.
*/
public void reset() {
byteCount = 0;
xBufOff = 0;
for (int i = 0; i < xBuf.length; i++)
{
xBuf[i] = 0;
}
}
} |
Partager