1 19 20 package com.maverick.crypto.digests; 21 22 26 public abstract class GeneralDigest 27 implements Digest { 28 private byte[] xBuf; 29 private int xBufOff; 30 31 private long byteCount; 32 33 36 protected GeneralDigest() { 37 xBuf = new byte[4]; 38 xBufOff = 0; 39 } 40 41 46 protected GeneralDigest(GeneralDigest t) { 47 xBuf = new byte[t.xBuf.length]; 48 System.arraycopy(t.xBuf, 0, xBuf, 0, t.xBuf.length); 49 50 xBufOff = t.xBufOff; 51 byteCount = t.byteCount; 52 } 53 54 public void update( 55 byte in) { 56 xBuf[xBufOff++] = in; 57 58 if (xBufOff == xBuf.length) { 59 processWord(xBuf, 0); 60 xBufOff = 0; 61 } 62 63 byteCount++; 64 } 65 66 public void update( 67 byte[] in, 68 int inOff, 69 int len) { 70 while ( (xBufOff != 0) && (len > 0)) { 74 update(in[inOff]); 75 76 inOff++; 77 len--; 78 } 79 80 while (len > xBuf.length) { 84 processWord(in, inOff); 85 86 inOff += xBuf.length; 87 len -= xBuf.length; 88 byteCount += xBuf.length; 89 } 90 91 while (len > 0) { 95 update(in[inOff]); 96 97 inOff++; 98 len--; 99 } 100 } 101 102 public void finish() { 103 long bitLength = (byteCount << 3); 104 105 update( (byte) 128); 109 110 while (xBufOff != 0) { 111 update( (byte) 0); 112 } 113 114 processLength(bitLength); 115 116 processBlock(); 117 } 118 119 public void reset() { 120 byteCount = 0; 121 122 xBufOff = 0; 123 for (int i = 0; i < xBuf.length; i++) { 124 xBuf[i] = 0; 125 } 126 } 127 128 protected abstract void processWord(byte[] in, int inOff); 129 130 protected abstract void processLength(long bitLength); 131 132 protected abstract void processBlock(); 133 134 public abstract int getDigestSize(); 135 136 public abstract int doFinal(byte[] output, int offset); 137 138 public abstract String getAlgorithmName(); 139 } 140 | Popular Tags |