1 /* SHA.java -- Class implementing the SHA-1 algorithm as specified in [1].
2 Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath 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, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.java
.security
.provider
;
41 import java
.security
.MessageDigest
;
44 This class implements the SHA-1 algorithm as described in [1].
46 [1] Federal Information Processing Standards Publication 180-1.
47 Specifications for the Secure Hash Standard. April 17, 1995.
49 @see java.security.MessageDigest
51 public class SHA
extends MessageDigest
implements Cloneable
59 public int engineGetDigestLength()
64 public void engineUpdate (byte b
)
66 int i
= ((int)bytecount
) & 0x3f; //wgs
67 int shift
= (3 - i
% 4) << 3;
71 W
[idx
] = (W
[idx
] & ~
(0xff << shift
)) | ((i
& 0xff) << shift
);
73 // if we've filled up a block, then process it
74 if (((++bytecount
) & 0x3f) == 0)
78 // This could be optimized.
79 public void engineUpdate (byte bytes
[], int off
, int len
)
82 throw new ArrayIndexOutOfBoundsException ();
86 engineUpdate (bytes
[off
++]);
89 public void engineReset ()
92 // magic numbers from [1] p. 10.
100 public byte[] engineDigest ()
102 long bitcount
= bytecount
<< 3;
103 engineUpdate ((byte)0x80); // 10000000 in binary; the start of the padding
105 // add the rest of the padding to fill this block out, but leave 8
106 // bytes to put in the original bytecount
107 while ((bytecount
& 0x3f) != 56)
108 engineUpdate ((byte)0);
110 // add the length of the original, unpadded block to the end of
112 W
[14] = (int)(bitcount
>>> 32);
113 W
[15] = (int)bitcount
;
116 // digest the fully padded block
120 = new byte[] {(byte)(H0
>>> 24), (byte)(H0
>>> 16),
121 (byte)(H0
>>> 8), (byte)H0
,
122 (byte)(H1
>>> 24), (byte)(H1
>>> 16),
123 (byte)(H1
>>> 8), (byte)H1
,
124 (byte)(H2
>>> 24), (byte)(H2
>>> 16),
125 (byte)(H2
>>> 8), (byte)H2
,
126 (byte)(H3
>>> 24), (byte)(H3
>>> 16),
127 (byte)(H3
>>> 8), (byte)H3
,
128 (byte)(H4
>>> 24), (byte)(H4
>>> 16),
129 (byte)(H4
>>> 8), (byte)H4
};
135 // Process a single block. This is pretty much copied verbatim from
137 private void munch ()
139 for (int t
= 16; t
< 80; ++ t
)
141 int Wt
= W
[t
- 3] ^ W
[t
- 8] ^ W
[t
- 14] ^ W
[t
- 16];
142 W
[t
] = Wt
<< 1 | Wt
>>> 31;
151 for (int t
= 0; t
< 20; ++ t
)
153 int TEMP
= (A
<< 5 | A
>>> 27) // S^5(A)
154 + ((B
& C
) | (~B
& D
)) // f_t(B,C,D)
160 C
= B
<< 30 | B
>>> 2; // S^30(B)
165 for (int t
= 20; t
< 40; ++ t
)
167 int TEMP
= (A
<< 5 | A
>>> 27) // S^5(A)
168 + (B ^ C ^ D
) // f_t(B,C,D)
174 C
= B
<< 30 | B
>>> 2; // S^30(B)
179 for (int t
= 40; t
< 60; ++ t
)
181 int TEMP
= (A
<< 5 | A
>>> 27) // S^5(A)
182 + (B
& C
| B
& D
| C
& D
) // f_t(B,C,D)
188 C
= B
<< 30 | B
>>> 2; // S^30(B)
193 for (int t
= 60; t
< 80; ++ t
)
195 int TEMP
= (A
<< 5 | A
>>> 27) // S^5(A)
196 + (B ^ C ^ D
) // f_t(B,C,D)
202 C
= B
<< 30 | B
>>> 2; // S^30(B)
213 // Reset W by clearing it.
214 for (int t
= 0; t
< 80; ++ t
)
218 public Object
clone ()
220 return new SHA (this);
223 private SHA (SHA copy
)
226 bytecount
= copy
.bytecount
;
232 System
.arraycopy (copy
.W
, 0, W
, 0, 80);
235 private final int W
[] = new int[80];
236 private long bytecount
;