source: other-projects/trunk/gs3-release-maker/tasks/sshtaskdef/src/mindbright/security/SHA1.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 5.1 KB
Line 
1/******************************************************************************
2 *
3 * Copyright (c) 1998-2000 by Mindbright Technology AB, Stockholm, Sweden.
4 * www.mindbright.se, [email protected]
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 *****************************************************************************
17 * $Author: mats $
18 * $Date: 2000/05/12 12:41:45 $
19 * $Name: rel1-2-1 $
20 *****************************************************************************/
21package mindbright.security;
22
23public final class SHA1 extends MessageDigest {
24 private int[] hash;
25 private int[] W;
26 private long count;
27 private int rest;
28 private byte[] buffer;
29
30 static byte padding[] = {
31 (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
34 };
35
36 private static int rotateLeft(int x, int n) {
37 return (x << n) | (x >>> (32 - n));
38 }
39
40 private static int F00_19(int x, int y, int z) {
41 return (((y ^ z) & x) ^ z) + 0x5a827999;
42 }
43
44 private static int F20_39(int x, int y, int z) {
45 return (x ^ y ^ z) + 0x6ed9eba1;
46 }
47
48 private static int F40_59(int x, int y, int z) {
49 return ((x & y) | ((x | y) & z)) + 0x8f1bbcdc;
50 }
51
52 private static int F60_79(int x, int y, int z) {
53 return (x ^ y ^ z) + 0xca62c1d6;
54 }
55
56 private void transform(byte data[], int offset) {
57 int a = hash[0];
58 int b = hash[1];
59 int c = hash[2];
60 int d = hash[3];
61 int e = hash[4];
62 int t, i;
63
64 for (i = 0; i < 16; i++) {
65 W[i] =
66 ((((int) (data[offset++] & 0xff)) << 24) |
67 (((int) (data[offset++] & 0xff)) << 16) |
68 (((int) (data[offset++] & 0xff)) << 8) |
69 (((int) (data[offset++] & 0xff))));
70 }
71
72 for(i = 16; i < 80; i++) {
73 t = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
74 W[i] = rotateLeft(t, 1);
75 }
76
77 for(i = 0; i < 20; i++) {
78 t = rotateLeft(a, 5) + F00_19(b, c, d) + e + W[i];
79 e = d;
80 d = c;
81 c = rotateLeft(b, 30);
82 b = a;
83 a = t;
84 }
85
86 for(i = 20; i < 40; i++) {
87 t = rotateLeft(a, 5) + F20_39(b, c, d) + e + W[i];
88 e = d;
89 d = c;
90 c = rotateLeft(b, 30);
91 b = a;
92 a = t;
93 }
94
95 for(i = 40; i < 60; i++) {
96 t = rotateLeft(a, 5) + F40_59(b, c, d) + e + W[i];
97 e = d;
98 d = c;
99 c = rotateLeft(b, 30);
100 b = a;
101 a = t;
102 }
103
104 for(i = 60; i < 80; i++) {
105 t = rotateLeft(a, 5) + F60_79(b, c, d) + e + W[i];
106 e = d;
107 d = c;
108 c = rotateLeft(b, 30);
109 b = a;
110 a = t;
111 }
112
113 hash[0] += a;
114 hash[1] += b;
115 hash[2] += c;
116 hash[3] += d;
117 hash[4] += e;
118 }
119
120 public SHA1() {
121 buffer = new byte[64];
122 hash = new int[5];
123 W = new int[80];
124 reset();
125 }
126
127 private SHA1(SHA1 c) {
128 buffer = new byte[64];
129 hash = new int[5];
130 W = new int[80];
131 System.arraycopy(c.hash, 0, hash, 0, 5);
132 System.arraycopy(c.buffer, 0, buffer, 0, 64);
133 count = c.count;
134 rest = c.rest;
135 }
136
137 public Object clone() {
138 return new SHA1(this);
139 }
140
141 public String getName() {
142 return "SHA1";
143 }
144
145 public void reset() {
146 hash[0] = 0x67452301;
147 hash[1] = 0xefcdab89;
148 hash[2] = 0x98badcfe;
149 hash[3] = 0x10325476;
150 hash[4] = 0xc3d2e1f0;
151 count = 0;
152 rest = 0;
153 }
154
155 public void update(byte[] data, int offset, int length) {
156 int left = 64 - rest;
157
158 count += length;
159
160 if(rest > 0 && length >= left) {
161 System.arraycopy(data, offset, buffer, rest, left);
162 transform(buffer, 0);
163 offset += left;
164 length -= left;
165 rest = 0;
166 }
167
168 while(length > 63) {
169 transform(data, offset);
170 offset += 64;
171 length -= 64;
172 }
173
174 if(length > 0) {
175 System.arraycopy(data, offset, buffer, rest, length);
176 rest += length;
177 }
178 }
179
180 public byte[] digest() {
181 byte[] buf = new byte[20];
182 digestInto(buf, 0);
183 return buf;
184 }
185
186 public int digestInto(byte[] dest, int destOff) {
187 int padlen = (rest < 56) ? (56 - rest) : (120 - rest);
188
189 count *= 8;
190 byte[] countBytes = {
191 (byte)(count >> 56),
192 (byte)(count >> 58),
193 (byte)(count >> 40),
194 (byte)(count >> 32),
195 (byte)(count >> 24),
196 (byte)(count >> 16),
197 (byte)(count >> 8),
198 (byte)(count)
199 };
200
201 update(padding, 0, padlen);
202 update(countBytes, 0, 8);
203
204 int i;
205 for (i = 0; i < 5; i++) {
206 dest[destOff++] = (byte) ((hash[i] >>> 24) & 0xff);
207 dest[destOff++] = (byte) ((hash[i] >>> 16) & 0xff);
208 dest[destOff++] = (byte) ((hash[i] >>> 8) & 0xff);
209 dest[destOff++] = (byte) ((hash[i]) & 0xff);
210 }
211
212 reset();
213 return 20;
214 }
215
216 public int blockSize() {
217 return 64;
218 }
219
220 public int hashSize() {
221 return 20;
222 }
223}
Note: See TracBrowser for help on using the repository browser.