12
12
* can determine if a file was manipulated, by comparing the message digest
13
13
* of the original file with the message digest of the file in question.
14
14
* Another example is the use of SHA-256 in the Proof-of-work algorithm of Bitcoin.
15
- *
15
+ * <p>
16
16
* This implementation is based on the RFC 6234 specification. The original
17
17
* specification of SHA-2 is defined in FIPS PUB 180-4. Due to the U.S.
18
18
* government shutdown by the end of 2018 the original specification was offline.
@@ -27,19 +27,20 @@ public final class Sha2 {
27
27
* By specification, the user-provided data can have a length of 0 <= L < 2^61 byte.
28
28
* The JVM, though, allows an array with a maximum length of approximately
29
29
* Integer.MAX_VALUE.</p>
30
+ *
30
31
* @param data the data/message to be digested
31
32
* @return the message digest with a fixed length of 224 bit (28 byte)
32
33
*/
33
34
public static String SHA224 (byte [] data ) {
34
35
final int [] initialHash = {
35
- 0xc1059ed8 , 0x367cd507 , 0x3070dd17 , 0xf70e5939 ,
36
- 0xffc00b31 , 0x68581511 , 0x64f98fa7 , 0xbefa4fa4
36
+ 0xc1059ed8 , 0x367cd507 , 0x3070dd17 , 0xf70e5939 ,
37
+ 0xffc00b31 , 0x68581511 , 0x64f98fa7 , 0xbefa4fa4
37
38
};
38
39
39
40
int [] finalHash = digest (data , initialHash );
40
41
41
42
StringBuilder builder = new StringBuilder ();
42
- for (int i = 0 ; i < finalHash .length - 1 ; i ++) {
43
+ for (int i = 0 ; i < finalHash .length - 1 ; i ++) {
43
44
builder .append (String .format ("%1$08x" , finalHash [i ]));
44
45
}
45
46
@@ -48,14 +49,15 @@ public static String SHA224(byte[] data) {
48
49
49
50
/**
50
51
* <p>Returns a SHA-256 message digest with a fixed length of 256 bit (32 byte).<p>
52
+ *
51
53
* @param data the data/message to be digested
52
54
* @return the message digest with a fixed length of 256 bit (32 byte)
53
55
* @see src.main.java.com.crypto.hash.Sha2#SHA224(byte[]) SHA224()
54
56
*/
55
57
public static String SHA256 (byte [] data ) {
56
58
final int [] initialHash = {
57
- 0x6a09e667 , 0xbb67ae85 , 0x3c6ef372 , 0xa54ff53a ,
58
- 0x510e527f , 0x9b05688c , 0x1f83d9ab , 0x5be0cd19
59
+ 0x6a09e667 , 0xbb67ae85 , 0x3c6ef372 , 0xa54ff53a ,
60
+ 0x510e527f , 0x9b05688c , 0x1f83d9ab , 0x5be0cd19
59
61
};
60
62
61
63
int [] finalHash = digest (data , initialHash );
@@ -73,19 +75,20 @@ public static String SHA256(byte[] data) {
73
75
* By specification, the user-provided data can have a length of 0 <= L < 2^125 byte.
74
76
* The JVM, though, allows an array with a maximum length of approximately
75
77
* Integer.MAX_VALUE.</p>
78
+ *
76
79
* @param data the data/message to be digested
77
80
* @return the message digest with a fixed length of 384 bit (48 byte)
78
81
*/
79
82
public static String SHA384 (byte [] data ) {
80
83
final long [] initialHash = {
81
- 0xcbbb9d5dc1059ed8L , 0x629a292a367cd507L , 0x9159015a3070dd17L , 0x152fecd8f70e5939L ,
82
- 0x67332667ffc00b31L , 0x8eb44a8768581511L , 0xdb0c2e0d64f98fa7L , 0x47b5481dbefa4fa4L
84
+ 0xcbbb9d5dc1059ed8L , 0x629a292a367cd507L , 0x9159015a3070dd17L , 0x152fecd8f70e5939L ,
85
+ 0x67332667ffc00b31L , 0x8eb44a8768581511L , 0xdb0c2e0d64f98fa7L , 0x47b5481dbefa4fa4L
83
86
};
84
87
85
88
long [] finalHash = digest (data , initialHash );
86
89
87
90
StringBuilder builder = new StringBuilder ();
88
- for (int i = 0 ; i < finalHash .length - 2 ; i ++) {
91
+ for (int i = 0 ; i < finalHash .length - 2 ; i ++) {
89
92
builder .append (String .format ("%1$016x" , finalHash [i ]));
90
93
}
91
94
@@ -94,14 +97,15 @@ public static String SHA384(byte[] data) {
94
97
95
98
/**
96
99
* <p>Returns a SHA-512 message digest with a fixed length of 512 bit (64 byte).</p>
100
+ *
97
101
* @param data the data/message to be digested
98
102
* @return the message digest with a fixed length of 512 bit (64 byte)
99
103
* @see src.main.java.com.crypto.hash.Sha2#SHA384(byte[]) SHA384()
100
104
*/
101
105
public static String SHA512 (byte [] data ) {
102
106
final long [] initialHash = {
103
- 0x6a09e667f3bcc908L , 0xbb67ae8584caa73bL , 0x3c6ef372fe94f82bL , 0xa54ff53a5f1d36f1L ,
104
- 0x510e527fade682d1L , 0x9b05688c2b3e6c1fL , 0x1f83d9abfb41bd6bL , 0x5be0cd19137e2179L
107
+ 0x6a09e667f3bcc908L , 0xbb67ae8584caa73bL , 0x3c6ef372fe94f82bL , 0xa54ff53a5f1d36f1L ,
108
+ 0x510e527fade682d1L , 0x9b05688c2b3e6c1fL , 0x1f83d9abfb41bd6bL , 0x5be0cd19137e2179L
105
109
};
106
110
107
111
long [] finalHash = digest (data , initialHash );
@@ -119,6 +123,7 @@ public static String SHA512(byte[] data) {
119
123
* <p>This method is wrapped by SHA224() and SHA256(). Both algorithms differ
120
124
* only in two points: the initialization hashes are different and for SHA-224
121
125
* the raw message digest is truncated by 1 byte.</p>
126
+ *
122
127
* @param data the data/message to be digested
123
128
* @param hash the initial hash value, which in the process gets used
124
129
* for the intermediate hashes
@@ -151,6 +156,7 @@ private static int[] digest(byte[] data, int[] hash) {
151
156
* <p>This method is wrapped by SHA384() and SHA512(). Both algorithms differ
152
157
* only in two points: the initialization hashes are different and for SHA-384
153
158
* the raw message digest is truncated by 2 byte.</p>
159
+ *
154
160
* @param data the data/message to be digested
155
161
* @param hash the initial hash value, which in the process gets used
156
162
* for the intermediate hashes
@@ -180,7 +186,8 @@ private static long[] digest(byte[] data, long[] hash) {
180
186
181
187
/**
182
188
* <p>Pads the user-provided data.</p>
183
- * @param data the data/message to be digested
189
+ *
190
+ * @param data the data/message to be digested
184
191
* @param blockSize the size of a data block (64 or 128 byte)
185
192
* @return the padding for the data
186
193
* @see <a href="https://tools.ietf.org/html/rfc6234#section-4">RFC 6234 - Message padding</a>
@@ -189,7 +196,7 @@ private static byte[] pad(byte[] data, int blockSize) {
189
196
byte [] padding ;
190
197
int lastBlockLength = data .length % blockSize ;
191
198
if (lastBlockLength + 1 > (blockSize / 8 ) * 7 ) {
192
- padding = new byte [blockSize * 2 - lastBlockLength ];
199
+ padding = new byte [blockSize * 2 - lastBlockLength ];
193
200
} else {
194
201
padding = new byte [blockSize - lastBlockLength ];
195
202
}
@@ -206,8 +213,9 @@ private static byte[] pad(byte[] data, int blockSize) {
206
213
207
214
/**
208
215
* Scrambles data blocks in a deterministic way.
216
+ *
209
217
* @param dataBlock the data blocks to be scrambled
210
- * @param hash the resulting hash
218
+ * @param hash the resulting hash
211
219
* @see <a href="https://tools.ietf.org/html/rfc6234#section-6.2">SHA-224 and SHA-256 Processing</a>
212
220
*/
213
221
private static void hashBlock (int [] dataBlock , int [] hash ) {
@@ -221,7 +229,7 @@ private static void hashBlock(int[] dataBlock, int[] hash) {
221
229
W [i ] = dataBlock [i ];
222
230
}
223
231
for (int i = 16 ; i < 64 ; i ++) {
224
- W [i ] = SSIG1 (W [i - 2 ]) + W [i - 7 ] + SSIG0 (W [i - 15 ]) + W [i - 16 ];
232
+ W [i ] = SSIG1 (W [i - 2 ]) + W [i - 7 ] + SSIG0 (W [i - 15 ]) + W [i - 16 ];
225
233
}
226
234
227
235
// Initialize the working variables
@@ -251,8 +259,9 @@ private static void hashBlock(int[] dataBlock, int[] hash) {
251
259
252
260
/**
253
261
* Scrambles data blocks in a deterministic way.
262
+ *
254
263
* @param dataBlock the data blocks to be scrambled
255
- * @param hash the resulting hash
264
+ * @param hash the resulting hash
256
265
* @see <a href="https://tools.ietf.org/html/rfc6234#section-6.4">SHA-384 and SHA-512 Processing</a>
257
266
*/
258
267
private static void hashBlock (long [] dataBlock , long [] hash ) {
@@ -266,7 +275,7 @@ private static void hashBlock(long[] dataBlock, long[] hash) {
266
275
W [i ] = dataBlock [i ];
267
276
}
268
277
for (int i = 16 ; i < 80 ; i ++) {
269
- W [i ] = SSIG1 (W [i - 2 ]) + W [i - 7 ] + SSIG0 (W [i - 15 ]) + W [i - 16 ];
278
+ W [i ] = SSIG1 (W [i - 2 ]) + W [i - 7 ] + SSIG0 (W [i - 15 ]) + W [i - 16 ];
270
279
}
271
280
272
281
// Initialize the working variables
@@ -396,4 +405,4 @@ private static int ROTR(int x, int n) {
396
405
private static long ROTR (long x , long n ) {
397
406
return (x >>> n ) | (x << (64 - n ));
398
407
}
399
- }
408
+ }
0 commit comments