diff options
Diffstat (limited to 'libjava/classpath/gnu/java/security/hash/MD2.java')
-rw-r--r-- | libjava/classpath/gnu/java/security/hash/MD2.java | 163 |
1 files changed, 59 insertions, 104 deletions
diff --git a/libjava/classpath/gnu/java/security/hash/MD2.java b/libjava/classpath/gnu/java/security/hash/MD2.java index 2a93c42c44a..8f6b265b037 100644 --- a/libjava/classpath/gnu/java/security/hash/MD2.java +++ b/libjava/classpath/gnu/java/security/hash/MD2.java @@ -42,13 +42,12 @@ import gnu.java.security.Registry; import gnu.java.security.util.Util; /** - * <p>An implementation of the MD2 message digest algorithm.</p> - * - * <p>MD2 is not widely used. Unless it is needed for compatibility with - * existing systems, it is not recommended for use in new applications.</p> - * - * <p>References:</p> - * + * An implementation of the MD2 message digest algorithm. + * <p> + * MD2 is not widely used. Unless it is needed for compatibility with + * existing systems, it is not recommended for use in new applications. + * <p> + * References: * <ol> * <li>The <a href="http://www.ietf.org/rfc/rfc1319.txt">MD2</a> * Message-Digest Algorithm.<br> @@ -57,12 +56,9 @@ import gnu.java.security.util.Util; * under section RFC 1319.</li> * </ol> */ -public class MD2 extends BaseHash +public class MD2 + extends BaseHash { - - // Constants and variables - // ------------------------------------------------------------------------- - /** An MD2 message digest is always 128-bits long, or 16 bytes. */ private static final int DIGEST_LENGTH = 16; @@ -70,34 +66,39 @@ public class MD2 extends BaseHash private static final int BLOCK_LENGTH = 16; /** 256 byte "random" permutation of the digits of pi. */ - private static final byte[] PI = { 41, 46, 67, -55, -94, -40, 124, 1, 61, 54, - 84, -95, -20, -16, 6, 19, 98, -89, 5, -13, - -64, -57, 115, -116, -104, -109, 43, -39, - -68, 76, -126, -54, 30, -101, 87, 60, -3, - -44, -32, 22, 103, 66, 111, 24, -118, 23, - -27, 18, -66, 78, -60, -42, -38, -98, -34, - 73, -96, -5, -11, -114, -69, 47, -18, 122, - -87, 104, 121, -111, 21, -78, 7, 63, -108, - -62, 16, -119, 11, 34, 95, 33, -128, 127, - 93, -102, 90, -112, 50, 39, 53, 62, -52, - -25, -65, -9, -105, 3, -1, 25, 48, -77, 72, - -91, -75, -47, -41, 94, -110, 42, -84, 86, - -86, -58, 79, -72, 56, -46, -106, -92, 125, - -74, 118, -4, 107, -30, -100, 116, 4, -15, - 69, -99, 112, 89, 100, 113, -121, 32, -122, - 91, -49, 101, -26, 45, -88, 2, 27, 96, 37, - -83, -82, -80, -71, -10, 28, 70, 97, 105, - 52, 64, 126, 15, 85, 71, -93, 35, -35, 81, - -81, 58, -61, 92, -7, -50, -70, -59, -22, - 38, 44, 83, 13, 110, -123, 40, -124, 9, - -45, -33, -51, -12, 65, -127, 77, 82, 106, - -36, 55, -56, 108, -63, -85, -6, 36, -31, - 123, 8, 12, -67, -79, 74, 120, -120, -107, - -117, -29, 99, -24, 109, -23, -53, -43, -2, - 59, 0, 29, 57, -14, -17, -73, 14, 102, 88, - -48, -28, -90, 119, 114, -8, -21, 117, 75, - 10, 49, 68, 80, -76, -113, -19, 31, 26, - -37, -103, -115, 51, -97, 17, -125, 20 }; + private static final byte[] PI = { + 41, 46, 67, -55, -94, -40, 124, 1, + 61, 54, 84, -95, -20, -16, 6, 19, + 98, -89, 5, -13, -64, -57, 115, -116, + -104, -109, 43, -39, -68, 76, -126, -54, + 30, -101, 87, 60, -3, -44, -32, 22, + 103, 66, 111, 24, -118, 23, -27, 18, + -66, 78, -60, -42, -38, -98, -34, 73, + -96, -5, -11, -114, -69, 47, -18, 122, + -87, 104, 121, -111, 21, -78, 7, 63, + -108, -62, 16, -119, 11, 34, 95, 33, + -128, 127, 93, -102, 90, -112, 50, 39, + 53, 62, -52, -25, -65, -9, -105, 3, + -1, 25, 48, -77, 72, -91, -75, -47, + -41, 94, -110, 42, -84, 86, -86, -58, + 79, -72, 56, -46, -106, -92, 125, -74, + 118, -4, 107, -30, -100, 116, 4, -15, + 69, -99, 112, 89, 100, 113, -121, 32, + -122, 91, -49, 101, -26, 45, -88, 2, + 27, 96, 37, -83, -82, -80, -71, -10, + 28, 70, 97, 105, 52, 64, 126, 15, + 85, 71, -93, 35, -35, 81, -81, 58, + -61, 92, -7, -50, -70, -59, -22, 38, + 44, 83, 13, 110, -123, 40, -124, 9, + -45, -33, -51, -12, 65, -127, 77, 82, + 106, -36, 55, -56, 108, -63, -85, -6, + 36, -31, 123, 8, 12, -67, -79, 74, + 120, -120, -107, -117, -29, 99, -24, 109, + -23, -53, -43, -2, 59, 0, 29, 57, + -14, -17, -73, 14, 102, 88, -48, -28, + -90, 119, 114, -8, -21, 117, 75, 10, + 49, 68, 80, -76, -113, -19, 31, 26, + -37, -103, -115, 51, - 97, 17, -125, 20 }; /** The output of this message digest when no data has been input. */ private static final String DIGEST0 = "8350E5A3E24C153DF2275C9F80692773"; @@ -114,9 +115,6 @@ public class MD2 extends BaseHash */ private byte[] work; - // Constructor(s) - // ------------------------------------------------------------------------- - /** Creates a new MD2 digest ready for use. */ public MD2() { @@ -124,7 +122,7 @@ public class MD2 extends BaseHash } /** - * <p>Private constructor used for cloning.</p> + * Private constructor used for cloning. * * @param md2 the instance to clone. */ @@ -135,38 +133,23 @@ public class MD2 extends BaseHash // superclass field this.count = md2.count; this.buffer = (byte[]) md2.buffer.clone(); - // private field this.checksum = (byte[]) md2.checksum.clone(); this.work = (byte[]) md2.work.clone(); } - // Class methods - // ------------------------------------------------------------------------- - - // Instance methods - // ------------------------------------------------------------------------- - - // java.lang.Cloneable interface implementation ---------------------------- - public Object clone() { return new MD2(this); } - // Implementation of abstract methods in BaseHash -------------------------- - protected byte[] getResult() { byte[] result = new byte[DIGEST_LENGTH]; - // Encrypt checksum as last block. encryptBlock(checksum, 0); - for (int i = 0; i < BLOCK_LENGTH; i++) - { - result[i] = work[i]; - } + result[i] = work[i]; return result; } @@ -181,17 +164,18 @@ public class MD2 extends BaseHash { if (valid == null) { - valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new MD2().digest()))); + String d = Util.toString(new MD2().digest()); + valid = Boolean.valueOf(DIGEST0.equals(d)); } return valid.booleanValue(); } /** - * <p>Generates an array of padding bytes. The padding is defined as + * Generates an array of padding bytes. The padding is defined as * <code>i</code> bytes of value <code>i</code>, where <code>i</code> is the * number of bytes to fill the last block of the message to * <code>BLOCK_LENGTH</code> bytes (or <code>BLOCK_LENGTH</code> bytes when - * the last block was completely full).</p> + * the last block was completely full). * * @return the bytes to pad the remaining bytes in the buffer before * completing a hash operation. @@ -200,47 +184,26 @@ public class MD2 extends BaseHash { int length = BLOCK_LENGTH - (int) (count % BLOCK_LENGTH); if (length == 0) - { - length = BLOCK_LENGTH; - } + length = BLOCK_LENGTH; + byte[] pad = new byte[length]; for (int i = 0; i < length; i++) - { - pad[i] = (byte) length; - } + pad[i] = (byte) length; + return pad; } /** - * <p>Adds <code>BLOCK_LENGTH</code> bytes to the running digest.</p> + * Adds <code>BLOCK_LENGTH</code> bytes to the running digest. * * @param in the byte array to take the <code>BLOCK_LENGTH</code> bytes from. * @param off the offset to start from in the given byte array. */ protected void transform(byte[] in, int off) { - // encryptBlock(in, off); - // updateCheckSum(in, off); updateCheckSumAndEncryptBlock(in, off); } - // Private instance methods ------------------------------------------------ - - /** - * Updates the checksum with the <code>BLOCK_LENGTH</code> bytes from the - * given array starting at <code>off</code>. - */ - /* - private void updateCheckSum(byte[] in, int off) { - byte l = checksum[BLOCK_LENGTH-1]; - for (int i = 0; i < BLOCK_LENGTH; i++) { - byte b = in[off+i]; - // l = (byte)((checksum[i] & 0xFF) ^ (PI[((b & 0xFF) ^ (l & 0xFF))] & 0xFF)); - l = (byte)(checksum[i] ^ PI[(b ^ l) & 0xFF]); - checksum[i] = l; - } - } - */ /** * Adds a new block (<code>BLOCK_LENGTH</code> bytes) to the running digest * from the given byte array starting from the given offset. @@ -251,20 +214,17 @@ public class MD2 extends BaseHash { byte b = in[off + i]; work[BLOCK_LENGTH + i] = b; - work[BLOCK_LENGTH * 2 + i] = (byte) (work[i] ^ b); + work[BLOCK_LENGTH * 2 + i] = (byte)(work[i] ^ b); } - byte t = 0; for (int i = 0; i < 18; i++) { for (int j = 0; j < 3 * BLOCK_LENGTH; j++) { - // t = (byte)((work[j] & 0xFF) ^ (PI[t & 0xFF] & 0xFF)); - t = (byte) (work[j] ^ PI[t & 0xFF]); + t = (byte)(work[j] ^ PI[t & 0xFF]); work[j] = t; } - // t = (byte)((t + i) & 0xFF); - t = (byte) (t + i); + t = (byte)(t + i); } } @@ -278,24 +238,19 @@ public class MD2 extends BaseHash { byte b = in[off + i]; work[BLOCK_LENGTH + i] = b; - // work[BLOCK_LENGTH*2+i] = (byte)((work[i] & 0xFF) ^ (b & 0xFF)); - work[BLOCK_LENGTH * 2 + i] = (byte) (work[i] ^ b); - // l = (byte)((checksum[i] & 0xFF) ^ (PI[((b & 0xFF) ^ (l & 0xFF))] & 0xFF)); - l = (byte) (checksum[i] ^ PI[(b ^ l) & 0xFF]); + work[BLOCK_LENGTH * 2 + i] = (byte)(work[i] ^ b); + l = (byte)(checksum[i] ^ PI[(b ^ l) & 0xFF]); checksum[i] = l; } - byte t = 0; for (int i = 0; i < 18; i++) { for (int j = 0; j < 3 * BLOCK_LENGTH; j++) { - // t = (byte)((work[j] & 0xFF) ^ (PI[t & 0xFF] & 0xFF)); - t = (byte) (work[j] ^ PI[t & 0xFF]); + t = (byte)(work[j] ^ PI[t & 0xFF]); work[j] = t; } - // t = (byte)((t + i) & 0xFF); - t = (byte) (t + i); + t = (byte)(t + i); } } } |