1 /* Copyright (c) 2000 The Legion Of The Bouncy Castle
2 * (http://www.bouncycastle.org)
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
23 package com.brian_web.crypto;
26 * implementation of SHA-1 as outlined in "Handbook of Applied Cryptography", pages 346 - 349.
28 * It is interesting to ponder why the, apart from the extra IV, the other difference here from MD5
29 * is the "endienness" of the word processing!
34 private static final int DIGEST_LENGTH = 20;
36 private int H1, H2, H3, H4, H5;
38 private int[] X = new int[80];
42 * Standard constructor
49 public int getDigestSize()
54 protected void processWord(
58 X[xOff++] = ((in[inOff] & 0xff) << 24) | ((in[inOff + 1] & 0xff) << 16)
59 | ((in[inOff + 2] & 0xff) << 8) | ((in[inOff + 3] & 0xff));
67 private void unpackWord(
72 out[outOff] = (byte)(word >>> 24);
73 out[outOff + 1] = (byte)(word >>> 16);
74 out[outOff + 2] = (byte)(word >>> 8);
75 out[outOff + 3] = (byte)word;
78 protected void processLength(
86 X[14] = (int)(bitLength >>> 32);
87 X[15] = (int)(bitLength & 0xffffffff);
96 unpackWord(H1, out, outOff);
97 unpackWord(H2, out, outOff + 4);
98 unpackWord(H3, out, outOff + 8);
99 unpackWord(H4, out, outOff + 12);
100 unpackWord(H5, out, outOff + 16);
106 * reset the chaining variables
119 for (int i = 0; i != X.length; i++)
130 return ((u & v) | ((~u) & w));
146 return ((u & v) | (u & w) | (v & w));
149 private int rotateLeft(
153 return (x << n) | (x >>> (32 - n));
156 protected void processBlock()
159 // expand 16 word block into 80 word block.
161 for (int i = 16; i <= 79; i++)
163 X[i] = rotateLeft((X[i - 3] ^ X[i - 8] ^ X[i - 14] ^ X[i - 16]), 1);
167 // set up working variables.
178 for (int j = 0; j <= 19; j++)
180 int t = rotateLeft(A, 5) + f(B, C, D) + E + X[j] + 0x5a827999;
184 C = rotateLeft(B, 30);
192 for (int j = 20; j <= 39; j++)
194 int t = rotateLeft(A, 5) + h(B, C, D) + E + X[j] + 0x6ed9eba1;
198 C = rotateLeft(B, 30);
206 for (int j = 40; j <= 59; j++)
208 int t = rotateLeft(A, 5) + g(B, C, D) + E + X[j] + 0x8f1bbcdc;
212 C = rotateLeft(B, 30);
220 for (int j = 60; j <= 79; j++)
222 int t = rotateLeft(A, 5) + h(B, C, D) + E + X[j] + 0xca62c1d6;
226 C = rotateLeft(B, 30);
238 // reset the offset and clean out the word buffer.
241 for (int i = 0; i != X.length; i++)