1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44:
59: public class MD2
60: extends BaseHash
61: {
62:
63: private static final int DIGEST_LENGTH = 16;
64:
65:
66: private static final int BLOCK_LENGTH = 16;
67:
68:
69: private static final byte[] PI = {
70: 41, 46, 67, -55, -94, -40, 124, 1,
71: 61, 54, 84, -95, -20, -16, 6, 19,
72: 98, -89, 5, -13, -64, -57, 115, -116,
73: -104, -109, 43, -39, -68, 76, -126, -54,
74: 30, -101, 87, 60, -3, -44, -32, 22,
75: 103, 66, 111, 24, -118, 23, -27, 18,
76: -66, 78, -60, -42, -38, -98, -34, 73,
77: -96, -5, -11, -114, -69, 47, -18, 122,
78: -87, 104, 121, -111, 21, -78, 7, 63,
79: -108, -62, 16, -119, 11, 34, 95, 33,
80: -128, 127, 93, -102, 90, -112, 50, 39,
81: 53, 62, -52, -25, -65, -9, -105, 3,
82: -1, 25, 48, -77, 72, -91, -75, -47,
83: -41, 94, -110, 42, -84, 86, -86, -58,
84: 79, -72, 56, -46, -106, -92, 125, -74,
85: 118, -4, 107, -30, -100, 116, 4, -15,
86: 69, -99, 112, 89, 100, 113, -121, 32,
87: -122, 91, -49, 101, -26, 45, -88, 2,
88: 27, 96, 37, -83, -82, -80, -71, -10,
89: 28, 70, 97, 105, 52, 64, 126, 15,
90: 85, 71, -93, 35, -35, 81, -81, 58,
91: -61, 92, -7, -50, -70, -59, -22, 38,
92: 44, 83, 13, 110, -123, 40, -124, 9,
93: -45, -33, -51, -12, 65, -127, 77, 82,
94: 106, -36, 55, -56, 108, -63, -85, -6,
95: 36, -31, 123, 8, 12, -67, -79, 74,
96: 120, -120, -107, -117, -29, 99, -24, 109,
97: -23, -53, -43, -2, 59, 0, 29, 57,
98: -14, -17, -73, 14, 102, 88, -48, -28,
99: -90, 119, 114, -8, -21, 117, 75, 10,
100: 49, 68, 80, -76, -113, -19, 31, 26,
101: -37, -103, -115, 51, - 97, 17, -125, 20 };
102:
103:
104: private static final String DIGEST0 = "8350E5A3E24C153DF2275C9F80692773";
105:
106:
107: private static Boolean valid;
108:
109:
110: private byte[] checksum;
111:
112:
116: private byte[] work;
117:
118:
119: public MD2()
120: {
121: super(Registry.MD2_HASH, DIGEST_LENGTH, BLOCK_LENGTH);
122: }
123:
124:
129: private MD2(MD2 md2)
130: {
131: this();
132:
133:
134: this.count = md2.count;
135: this.buffer = (byte[]) md2.buffer.clone();
136:
137: this.checksum = (byte[]) md2.checksum.clone();
138: this.work = (byte[]) md2.work.clone();
139: }
140:
141: public Object clone()
142: {
143: return new MD2(this);
144: }
145:
146: protected byte[] getResult()
147: {
148: byte[] result = new byte[DIGEST_LENGTH];
149:
150: encryptBlock(checksum, 0);
151: for (int i = 0; i < BLOCK_LENGTH; i++)
152: result[i] = work[i];
153:
154: return result;
155: }
156:
157: protected void resetContext()
158: {
159: checksum = new byte[BLOCK_LENGTH];
160: work = new byte[BLOCK_LENGTH * 3];
161: }
162:
163: public boolean selfTest()
164: {
165: if (valid == null)
166: {
167: String d = Util.toString(new MD2().digest());
168: valid = Boolean.valueOf(DIGEST0.equals(d));
169: }
170: return valid.booleanValue();
171: }
172:
173:
183: protected byte[] padBuffer()
184: {
185: int length = BLOCK_LENGTH - (int) (count % BLOCK_LENGTH);
186: if (length == 0)
187: length = BLOCK_LENGTH;
188:
189: byte[] pad = new byte[length];
190: for (int i = 0; i < length; i++)
191: pad[i] = (byte) length;
192:
193: return pad;
194: }
195:
196:
202: protected void transform(byte[] in, int off)
203: {
204: updateCheckSumAndEncryptBlock(in, off);
205: }
206:
207:
211: private void encryptBlock(byte[] in, int off)
212: {
213: for (int i = 0; i < BLOCK_LENGTH; i++)
214: {
215: byte b = in[off + i];
216: work[BLOCK_LENGTH + i] = b;
217: work[BLOCK_LENGTH * 2 + i] = (byte)(work[i] ^ b);
218: }
219: byte t = 0;
220: for (int i = 0; i < 18; i++)
221: {
222: for (int j = 0; j < 3 * BLOCK_LENGTH; j++)
223: {
224: t = (byte)(work[j] ^ PI[t & 0xFF]);
225: work[j] = t;
226: }
227: t = (byte)(t + i);
228: }
229: }
230:
231:
234: private void updateCheckSumAndEncryptBlock(byte[] in, int off)
235: {
236: byte l = checksum[BLOCK_LENGTH - 1];
237: for (int i = 0; i < BLOCK_LENGTH; i++)
238: {
239: byte b = in[off + i];
240: work[BLOCK_LENGTH + i] = b;
241: work[BLOCK_LENGTH * 2 + i] = (byte)(work[i] ^ b);
242: l = (byte)(checksum[i] ^ PI[(b ^ l) & 0xFF]);
243: checksum[i] = l;
244: }
245: byte t = 0;
246: for (int i = 0; i < 18; i++)
247: {
248: for (int j = 0; j < 3 * BLOCK_LENGTH; j++)
249: {
250: t = (byte)(work[j] ^ PI[t & 0xFF]);
251: work[j] = t;
252: }
253: t = (byte)(t + i);
254: }
255: }
256: }