diff options
Diffstat (limited to 'gnu/java/security/hash/RipeMD128.java')
-rw-r--r-- | gnu/java/security/hash/RipeMD128.java | 148 |
1 files changed, 57 insertions, 91 deletions
diff --git a/gnu/java/security/hash/RipeMD128.java b/gnu/java/security/hash/RipeMD128.java index 31874a0c2..62f5a05e5 100644 --- a/gnu/java/security/hash/RipeMD128.java +++ b/gnu/java/security/hash/RipeMD128.java @@ -42,54 +42,48 @@ import gnu.java.security.Registry; import gnu.java.security.util.Util; /** - * <p>RIPEMD-128 is a 128-bit message digest.</p> - * - * <p>References:</p> - * + * RIPEMD-128 is a 128-bit message digest. + * <p> + * References: * <ol> * <li><a href="http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html"> * RIPEMD160</a>: A Strengthened Version of RIPEMD.<br> * Hans Dobbertin, Antoon Bosselaers and Bart Preneel.</li> * </ol> */ -public class RipeMD128 extends BaseHash +public class RipeMD128 + extends BaseHash { - - // Constants and variables - // ------------------------------------------------------------------------- - private static final int BLOCK_SIZE = 64; // inner block size in bytes private static final String DIGEST0 = "CDF26213A150DC3ECB610F18F6B38B46"; /** Constants for the transform method. */ // selection of message word - private static final int[] R = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, - 13, 14, 15, 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, - 9, 5, 2, 14, 11, 8, 3, 10, 14, 4, 9, 15, 8, - 1, 2, 7, 0, 6, 13, 11, 5, 12, 1, 9, 11, 10, - 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2 }; - - private static final int[] Rp = { 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, - 10, 3, 12, 6, 11, 3, 7, 0, 13, 5, 10, 14, - 15, 8, 12, 4, 9, 1, 2, 15, 5, 1, 3, 7, 14, - 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 8, 6, 4, - 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14 }; + private static final int[] R = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, + 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, + 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2 }; + + private static final int[] Rp = { + 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, + 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, + 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, + 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14 }; // amount for rotate left (rol) - private static final int[] S = { 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, - 6, 7, 9, 8, 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, - 15, 9, 11, 7, 13, 12, 11, 13, 6, 7, 14, 9, - 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, 11, 12, - 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, - 12 }; - - private static final int[] Sp = { 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, - 14, 14, 12, 6, 9, 13, 15, 7, 12, 8, 9, 11, - 7, 7, 12, 7, 6, 15, 13, 11, 9, 7, 15, 11, 8, - 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, 15, - 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, - 15, 8 }; + private static final int[] S = { + 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, + 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, + 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, + 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12 }; + + private static final int[] Sp = { + 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, + 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, + 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, + 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8 }; /** caches the result of the correctness test, once executed. */ private static Boolean valid; @@ -100,9 +94,6 @@ public class RipeMD128 extends BaseHash /** 512 bits work buffer = 16 x 32-bit words */ private int[] X = new int[16]; - // Constructor(s) - // ------------------------------------------------------------------------- - /** Trivial 0-arguments constructor. */ public RipeMD128() { @@ -110,7 +101,7 @@ public class RipeMD128 extends BaseHash } /** - * <p>Private constructor for cloning purposes.</p> + * Private constructor for cloning purposes. * * @param md the instance to clone. */ @@ -126,40 +117,26 @@ public class RipeMD128 extends BaseHash this.buffer = (byte[]) md.buffer.clone(); } - // Class methods - // ------------------------------------------------------------------------- - - // Instance methods - // ------------------------------------------------------------------------- - - // java.lang.Cloneable interface implementation ---------------------------- - public Object clone() { return new RipeMD128(this); } - // Implementation of concrete methods in BaseHash -------------------------- - protected void transform(byte[] in, int offset) { int A, B, C, D, Ap, Bp, Cp, Dp, T, s, i; - - // encode 64 bytes from input block into an array of 16 unsigned - // integers. + // encode 64 bytes from input block into an array of 16 unsigned integers. for (i = 0; i < 16; i++) - { - X[i] = (in[offset++] & 0xFF) | (in[offset++] & 0xFF) << 8 - | (in[offset++] & 0xFF) << 16 | in[offset++] << 24; - } - + X[i] = (in[offset++] & 0xFF) + | (in[offset++] & 0xFF) << 8 + | (in[offset++] & 0xFF) << 16 + | in[offset++] << 24; A = Ap = h0; B = Bp = h1; C = Cp = h2; D = Dp = h3; - - for (i = 0; i < 16; i++) - { // rounds 0...15 + for (i = 0; i < 16; i++) // rounds 0...15 + { s = S[i]; T = A + (B ^ C ^ D) + X[i]; A = D; @@ -174,9 +151,8 @@ public class RipeMD128 extends BaseHash Cp = Bp; Bp = T << s | T >>> (32 - s); } - - for (; i < 32; i++) - { // rounds 16...31 + for (; i < 32; i++) // rounds 16...31 + { s = S[i]; T = A + ((B & C) | (~B & D)) + X[R[i]] + 0x5A827999; A = D; @@ -191,9 +167,8 @@ public class RipeMD128 extends BaseHash Cp = Bp; Bp = T << s | T >>> (32 - s); } - - for (; i < 48; i++) - { // rounds 32...47 + for (; i < 48; i++) // rounds 32...47 + { s = S[i]; T = A + ((B | ~C) ^ D) + X[R[i]] + 0x6ED9EBA1; A = D; @@ -208,9 +183,8 @@ public class RipeMD128 extends BaseHash Cp = Bp; Bp = T << s | T >>> (32 - s); } - - for (; i < 64; i++) - { // rounds 48...63 + for (; i < 64; i++) // rounds 48...63 + { s = S[i]; T = A + ((B & D) | (C & ~D)) + X[R[i]] + 0x8F1BBCDC; A = D; @@ -225,7 +199,6 @@ public class RipeMD128 extends BaseHash Cp = Bp; Bp = T << s | T >>> (32 - s); } - T = h1 + C + Dp; h1 = h2 + D + Ap; h2 = h3 + A + Bp; @@ -235,39 +208,32 @@ public class RipeMD128 extends BaseHash protected byte[] padBuffer() { - int n = (int) (count % BLOCK_SIZE); + int n = (int)(count % BLOCK_SIZE); int padding = (n < 56) ? (56 - n) : (120 - n); byte[] result = new byte[padding + 8]; - // padding is always binary 1 followed by binary 0s result[0] = (byte) 0x80; - // save number of bits, casting the long to an array of 8 bytes long bits = count << 3; result[padding++] = (byte) bits; - result[padding++] = (byte) (bits >>> 8); - result[padding++] = (byte) (bits >>> 16); - result[padding++] = (byte) (bits >>> 24); - result[padding++] = (byte) (bits >>> 32); - result[padding++] = (byte) (bits >>> 40); - result[padding++] = (byte) (bits >>> 48); - result[padding] = (byte) (bits >>> 56); - + result[padding++] = (byte)(bits >>> 8); + result[padding++] = (byte)(bits >>> 16); + result[padding++] = (byte)(bits >>> 24); + result[padding++] = (byte)(bits >>> 32); + result[padding++] = (byte)(bits >>> 40); + result[padding++] = (byte)(bits >>> 48); + result[padding ] = (byte)(bits >>> 56); return result; } protected byte[] getResult() { - byte[] result = new byte[] { (byte) h0, (byte) (h0 >>> 8), - (byte) (h0 >>> 16), (byte) (h0 >>> 24), - (byte) h1, (byte) (h1 >>> 8), - (byte) (h1 >>> 16), (byte) (h1 >>> 24), - (byte) h2, (byte) (h2 >>> 8), - (byte) (h2 >>> 16), (byte) (h2 >>> 24), - (byte) h3, (byte) (h3 >>> 8), - (byte) (h3 >>> 16), (byte) (h3 >>> 24) }; - - return result; + return new byte[] { + (byte) h0, (byte)(h0 >>> 8), (byte)(h0 >>> 16), (byte)(h0 >>> 24), + (byte) h1, (byte)(h1 >>> 8), (byte)(h1 >>> 16), (byte)(h1 >>> 24), + (byte) h2, (byte)(h2 >>> 8), (byte)(h2 >>> 16), (byte)(h2 >>> 24), + (byte) h3, (byte)(h3 >>> 8), (byte)(h3 >>> 16), (byte)(h3 >>> 24) + }; } protected void resetContext() @@ -283,8 +249,8 @@ public class RipeMD128 extends BaseHash { if (valid == null) { - valid = Boolean.valueOf - (DIGEST0.equals(Util.toString(new RipeMD128().digest()))); + String d = Util.toString(new RipeMD128().digest()); + valid = Boolean.valueOf(DIGEST0.equals(d)); } return valid.booleanValue(); } |