summaryrefslogtreecommitdiff
path: root/gnu/java/security/provider/SHA.java
diff options
context:
space:
mode:
authorAaron M. Renn <arenn@urbanophile.com>1999-07-18 21:36:51 +0000
committerAaron M. Renn <arenn@urbanophile.com>1999-07-18 21:36:51 +0000
commit11a56217114c8a391aa3d8bd274df8553d03633f (patch)
tree3ca4754241e8759062da193168ff13c0b9c752c3 /gnu/java/security/provider/SHA.java
parente9364fcf79b57d3c4f373ba618eea9ae9c022e13 (diff)
downloadclasspath-11a56217114c8a391aa3d8bd274df8553d03633f.tar.gz
Initial checkin of Mark Benvenuto's security code
Diffstat (limited to 'gnu/java/security/provider/SHA.java')
-rw-r--r--gnu/java/security/provider/SHA.java427
1 files changed, 216 insertions, 211 deletions
diff --git a/gnu/java/security/provider/SHA.java b/gnu/java/security/provider/SHA.java
index 8848a7bb0..ae6baa2a1 100644
--- a/gnu/java/security/provider/SHA.java
+++ b/gnu/java/security/provider/SHA.java
@@ -1,211 +1,216 @@
-/* SHA.java -- Class implementing the SHA-1 algorithm as specified in [1].
- Copyright (c) 1999 by Free Software Foundation, Inc.
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU Library General Public License as published
- by the Free Software Foundation, version 2. (see COPYING.LIB)
-
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software Foundation
- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA. */
-
-package gnu.java.security.provider;
-
-import java.security.MessageDigest;
-
-/**
- This class implements the SHA-1 algorithm as described in [1].
-
- [1] Federal Information Processing Standards Publication 180-1.
- Specifications for the Secure Hash Standard. April 17, 1995.
-
- @see java.security.MessageDigest
-*/
-public class SHA extends MessageDigest implements Cloneable
-{
- public SHA ()
- {
- super ("SHA");
- engineReset ();
- }
-
- public void engineUpdate (byte b)
- {
- int i = (int)bytecount % 64;
- int shift = (3 - i % 4) * 8;
- int idx = i / 4;
-
- // if you could index ints, this would be: W[idx][shift/8] = b
- W[idx] = (W[idx] & ~(0xff << shift)) | ((b & 0xff) << shift);
-
- // if we've filled up a block, then process it
- if ((++ bytecount) % 64 == 0)
- munch ();
- }
-
- // This could be optimized.
- public void engineUpdate (byte bytes[], int off, int len)
- {
- if (len < 0)
- throw new ArrayIndexOutOfBoundsException ();
-
- int end = off + len;
- while (off < end)
- engineUpdate (bytes[off++]);
- }
-
- public void engineReset ()
- {
- bytecount = 0;
- // magic numbers from [1] p. 10.
- H0 = 0x67452301;
- H1 = 0xefcdab89;
- H2 = 0x98badcfe;
- H3 = 0x10325476;
- H4 = 0xc3d2e1f0;
- }
-
- public byte[] engineDigest ()
- {
- long bitcount = bytecount * 8;
- engineUpdate ((byte)-128); // 10000000 in binary; the start of the padding
-
- // add the rest of the padding to fill this block out, but leave 8
- // bytes to put in the original bytecount
- while ((int)bytecount % 64 != 56)
- engineUpdate ((byte)0);
-
- // add the length of the original, unpadded block to the end of
- // the padding
- W[14] = (int)(bitcount >>> 32);
- W[15] = (int)bitcount;
- bytecount += 8;
-
- // digest the fully padded block
- munch ();
-
- byte[] result
- = new byte[] {(byte)(H0 >>> 24), (byte)(H0 >>> 16),
- (byte)(H0 >>> 8), (byte)H0,
- (byte)(H1 >>> 24), (byte)(H1 >>> 16),
- (byte)(H1 >>> 8), (byte)H1,
- (byte)(H2 >>> 24), (byte)(H2 >>> 16),
- (byte)(H2 >>> 8), (byte)H2,
- (byte)(H3 >>> 24), (byte)(H3 >>> 16),
- (byte)(H3 >>> 8), (byte)H3,
- (byte)(H4 >>> 24), (byte)(H4 >>> 16),
- (byte)(H4 >>> 8), (byte)H4};
-
- engineReset ();
- return result;
- }
-
- // Process a single block. This is pretty much copied verbatim from
- // [1] pp. 9, 10.
- private void munch ()
- {
- for (int t = 16; t < 80; ++ t)
- {
- int Wt = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
- W[t] = Wt << 1 | Wt >>> 31;
- }
-
- int A = H0;
- int B = H1;
- int C = H2;
- int D = H3;
- int E = H4;
-
- for (int t = 0; t < 20; ++ t)
- {
- int TEMP = (A << 5 | A >>> 27) // S^5(A)
- + ((B & C) | (~B & D)) // f_t(B,C,D)
- + E + W[t]
- + 0x5a827999; // K_t
-
- E = D;
- D = C;
- C = B << 30 | B >>> 2; // S^30(B)
- B = A;
- A = TEMP;
- }
-
- for (int t = 20; t < 40; ++ t)
- {
- int TEMP = (A << 5 | A >>> 27) // S^5(A)
- + (B ^ C ^ D) // f_t(B,C,D)
- + E + W[t]
- + 0x6ed9eba1; // K_t
-
- E = D;
- D = C;
- C = B << 30 | B >>> 2; // S^30(B)
- B = A;
- A = TEMP;
- }
-
- for (int t = 40; t < 60; ++ t)
- {
- int TEMP = (A << 5 | A >>> 27) // S^5(A)
- + (B & C | B & D | C & D) // f_t(B,C,D)
- + E + W[t]
- + 0x8f1bbcdc; // K_t
-
- E = D;
- D = C;
- C = B << 30 | B >>> 2; // S^30(B)
- B = A;
- A = TEMP;
- }
-
- for (int t = 60; t < 80; ++ t)
- {
- int TEMP = (A << 5 | A >>> 27) // S^5(A)
- + (B ^ C ^ D) // f_t(B,C,D)
- + E + W[t]
- + 0xca62c1d6; // K_t
-
- E = D;
- D = C;
- C = B << 30 | B >>> 2; // S^30(B)
- B = A;
- A = TEMP;
- }
-
- H0 += A;
- H1 += B;
- H2 += C;
- H3 += D;
- H4 += E;
- }
-
- public Object clone ()
- {
- return new SHA (this);
- }
-
- private SHA (SHA copy)
- {
- this ();
- bytecount = copy.bytecount;
- H0 = copy.H0;
- H1 = copy.H1;
- H2 = copy.H2;
- H3 = copy.H3;
- H4 = copy.H4;
- System.arraycopy (copy.W, 0, W, 0, 80);
- }
-
- private final int W[] = new int[80];
- private long bytecount;
- private int H0;
- private int H1;
- private int H2;
- private int H3;
- private int H4;
-}
+/* SHA.java -- Class implementing the SHA-1 algorithm as specified in [1].
+ Copyright (c) 1999 by Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Library General Public License as published
+ by the Free Software Foundation, version 2. (see COPYING.LIB)
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA. */
+
+package gnu.java.security.provider;
+
+import java.security.MessageDigest;
+
+/**
+ This class implements the SHA-1 algorithm as described in [1].
+
+ [1] Federal Information Processing Standards Publication 180-1.
+ Specifications for the Secure Hash Standard. April 17, 1995.
+
+ @see java.security.MessageDigest
+*/
+public class SHA extends MessageDigest implements Cloneable
+{
+ public SHA ()
+ {
+ super("SHA");
+ engineReset ();
+ }
+
+ public int engineGetDigestLength()
+ {
+ return 16;
+ }
+
+ public void engineUpdate (byte b)
+ {
+ int i = (int)bytecount % 64;
+ int shift = (3 - i % 4) * 8;
+ int idx = i / 4;
+
+ // if you could index ints, this would be: W[idx][shift/8] = b
+ W[idx] = (W[idx] & ~(0xff << shift)) | ((b & 0xff) << shift);
+
+ // if we've filled up a block, then process it
+ if ((++ bytecount) % 64 == 0)
+ munch ();
+ }
+
+ // This could be optimized.
+ public void engineUpdate (byte bytes[], int off, int len)
+ {
+ if (len < 0)
+ throw new ArrayIndexOutOfBoundsException ();
+
+ int end = off + len;
+ while (off < end)
+ engineUpdate (bytes[off++]);
+ }
+
+ public void engineReset ()
+ {
+ bytecount = 0;
+ // magic numbers from [1] p. 10.
+ H0 = 0x67452301;
+ H1 = 0xefcdab89;
+ H2 = 0x98badcfe;
+ H3 = 0x10325476;
+ H4 = 0xc3d2e1f0;
+ }
+
+ public byte[] engineDigest ()
+ {
+ long bitcount = bytecount * 8;
+ engineUpdate ((byte)0x80); // 10000000 in binary; the start of the padding
+
+ // add the rest of the padding to fill this block out, but leave 8
+ // bytes to put in the original bytecount
+ while ((int)bytecount % 64 != 56)
+ engineUpdate ((byte)0);
+
+ // add the length of the original, unpadded block to the end of
+ // the padding
+ W[14] = (int)(bitcount >>> 32);
+ W[15] = (int)bitcount;
+ bytecount += 8;
+
+ // digest the fully padded block
+ munch ();
+
+ byte[] result
+ = new byte[] {(byte)(H0 >>> 24), (byte)(H0 >>> 16),
+ (byte)(H0 >>> 8), (byte)H0,
+ (byte)(H1 >>> 24), (byte)(H1 >>> 16),
+ (byte)(H1 >>> 8), (byte)H1,
+ (byte)(H2 >>> 24), (byte)(H2 >>> 16),
+ (byte)(H2 >>> 8), (byte)H2,
+ (byte)(H3 >>> 24), (byte)(H3 >>> 16),
+ (byte)(H3 >>> 8), (byte)H3,
+ (byte)(H4 >>> 24), (byte)(H4 >>> 16),
+ (byte)(H4 >>> 8), (byte)H4};
+
+ engineReset ();
+ return result;
+ }
+
+ // Process a single block. This is pretty much copied verbatim from
+ // [1] pp. 9, 10.
+ private void munch ()
+ {
+ for (int t = 16; t < 80; ++ t)
+ {
+ int Wt = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
+ W[t] = Wt << 1 | Wt >>> 31;
+ }
+
+ int A = H0;
+ int B = H1;
+ int C = H2;
+ int D = H3;
+ int E = H4;
+
+ for (int t = 0; t < 20; ++ t)
+ {
+ int TEMP = (A << 5 | A >>> 27) // S^5(A)
+ + ((B & C) | (~B & D)) // f_t(B,C,D)
+ + E + W[t]
+ + 0x5a827999; // K_t
+
+ E = D;
+ D = C;
+ C = B << 30 | B >>> 2; // S^30(B)
+ B = A;
+ A = TEMP;
+ }
+
+ for (int t = 20; t < 40; ++ t)
+ {
+ int TEMP = (A << 5 | A >>> 27) // S^5(A)
+ + (B ^ C ^ D) // f_t(B,C,D)
+ + E + W[t]
+ + 0x6ed9eba1; // K_t
+
+ E = D;
+ D = C;
+ C = B << 30 | B >>> 2; // S^30(B)
+ B = A;
+ A = TEMP;
+ }
+
+ for (int t = 40; t < 60; ++ t)
+ {
+ int TEMP = (A << 5 | A >>> 27) // S^5(A)
+ + (B & C | B & D | C & D) // f_t(B,C,D)
+ + E + W[t]
+ + 0x8f1bbcdc; // K_t
+
+ E = D;
+ D = C;
+ C = B << 30 | B >>> 2; // S^30(B)
+ B = A;
+ A = TEMP;
+ }
+
+ for (int t = 60; t < 80; ++ t)
+ {
+ int TEMP = (A << 5 | A >>> 27) // S^5(A)
+ + (B ^ C ^ D) // f_t(B,C,D)
+ + E + W[t]
+ + 0xca62c1d6; // K_t
+
+ E = D;
+ D = C;
+ C = B << 30 | B >>> 2; // S^30(B)
+ B = A;
+ A = TEMP;
+ }
+
+ H0 += A;
+ H1 += B;
+ H2 += C;
+ H3 += D;
+ H4 += E;
+ }
+
+ public Object clone ()
+ {
+ return new SHA (this);
+ }
+
+ private SHA (SHA copy)
+ {
+ this ();
+ bytecount = copy.bytecount;
+ H0 = copy.H0;
+ H1 = copy.H1;
+ H2 = copy.H2;
+ H3 = copy.H3;
+ H4 = copy.H4;
+ System.arraycopy (copy.W, 0, W, 0, 80);
+ }
+
+ private final int W[] = new int[80];
+ private long bytecount;
+ private int H0;
+ private int H1;
+ private int H2;
+ private int H3;
+ private int H4;
+}