1212 * can determine if a file was manipulated, by comparing the message digest
1313 * of the original file with the message digest of the file in question.
1414 * Another example is the use of SHA-256 in the Proof-of-work algorithm of Bitcoin.
15- *
15+ * <p>
1616 * This implementation is based on the RFC 6234 specification. The original
1717 * specification of SHA-2 is defined in FIPS PUB 180-4. Due to the U.S.
1818 * government shutdown by the end of 2018 the original specification was offline.
@@ -27,19 +27,20 @@ public final class Sha2 {
2727 * By specification, the user-provided data can have a length of 0 <= L < 2^61 byte.
2828 * The JVM, though, allows an array with a maximum length of approximately
2929 * Integer.MAX_VALUE.</p>
30+ *
3031 * @param data the data/message to be digested
3132 * @return the message digest with a fixed length of 224 bit (28 byte)
3233 */
3334 public static String SHA224 (byte [] data ) {
3435 final int [] initialHash = {
35- 0xc1059ed8 , 0x367cd507 , 0x3070dd17 , 0xf70e5939 ,
36- 0xffc00b31 , 0x68581511 , 0x64f98fa7 , 0xbefa4fa4
36+ 0xc1059ed8 , 0x367cd507 , 0x3070dd17 , 0xf70e5939 ,
37+ 0xffc00b31 , 0x68581511 , 0x64f98fa7 , 0xbefa4fa4
3738 };
3839
3940 int [] finalHash = digest (data , initialHash );
4041
4142 StringBuilder builder = new StringBuilder ();
42- for (int i = 0 ; i < finalHash .length - 1 ; i ++) {
43+ for (int i = 0 ; i < finalHash .length - 1 ; i ++) {
4344 builder .append (String .format ("%1$08x" , finalHash [i ]));
4445 }
4546
@@ -48,14 +49,15 @@ public static String SHA224(byte[] data) {
4849
4950 /**
5051 * <p>Returns a SHA-256 message digest with a fixed length of 256 bit (32 byte).<p>
52+ *
5153 * @param data the data/message to be digested
5254 * @return the message digest with a fixed length of 256 bit (32 byte)
5355 * @see src.main.java.com.crypto.hash.Sha2#SHA224(byte[]) SHA224()
5456 */
5557 public static String SHA256 (byte [] data ) {
5658 final int [] initialHash = {
57- 0x6a09e667 , 0xbb67ae85 , 0x3c6ef372 , 0xa54ff53a ,
58- 0x510e527f , 0x9b05688c , 0x1f83d9ab , 0x5be0cd19
59+ 0x6a09e667 , 0xbb67ae85 , 0x3c6ef372 , 0xa54ff53a ,
60+ 0x510e527f , 0x9b05688c , 0x1f83d9ab , 0x5be0cd19
5961 };
6062
6163 int [] finalHash = digest (data , initialHash );
@@ -73,19 +75,20 @@ public static String SHA256(byte[] data) {
7375 * By specification, the user-provided data can have a length of 0 <= L < 2^125 byte.
7476 * The JVM, though, allows an array with a maximum length of approximately
7577 * Integer.MAX_VALUE.</p>
78+ *
7679 * @param data the data/message to be digested
7780 * @return the message digest with a fixed length of 384 bit (48 byte)
7881 */
7982 public static String SHA384 (byte [] data ) {
8083 final long [] initialHash = {
81- 0xcbbb9d5dc1059ed8L , 0x629a292a367cd507L , 0x9159015a3070dd17L , 0x152fecd8f70e5939L ,
82- 0x67332667ffc00b31L , 0x8eb44a8768581511L , 0xdb0c2e0d64f98fa7L , 0x47b5481dbefa4fa4L
84+ 0xcbbb9d5dc1059ed8L , 0x629a292a367cd507L , 0x9159015a3070dd17L , 0x152fecd8f70e5939L ,
85+ 0x67332667ffc00b31L , 0x8eb44a8768581511L , 0xdb0c2e0d64f98fa7L , 0x47b5481dbefa4fa4L
8386 };
8487
8588 long [] finalHash = digest (data , initialHash );
8689
8790 StringBuilder builder = new StringBuilder ();
88- for (int i = 0 ; i < finalHash .length - 2 ; i ++) {
91+ for (int i = 0 ; i < finalHash .length - 2 ; i ++) {
8992 builder .append (String .format ("%1$016x" , finalHash [i ]));
9093 }
9194
@@ -94,14 +97,15 @@ public static String SHA384(byte[] data) {
9497
9598 /**
9699 * <p>Returns a SHA-512 message digest with a fixed length of 512 bit (64 byte).</p>
100+ *
97101 * @param data the data/message to be digested
98102 * @return the message digest with a fixed length of 512 bit (64 byte)
99103 * @see src.main.java.com.crypto.hash.Sha2#SHA384(byte[]) SHA384()
100104 */
101105 public static String SHA512 (byte [] data ) {
102106 final long [] initialHash = {
103- 0x6a09e667f3bcc908L , 0xbb67ae8584caa73bL , 0x3c6ef372fe94f82bL , 0xa54ff53a5f1d36f1L ,
104- 0x510e527fade682d1L , 0x9b05688c2b3e6c1fL , 0x1f83d9abfb41bd6bL , 0x5be0cd19137e2179L
107+ 0x6a09e667f3bcc908L , 0xbb67ae8584caa73bL , 0x3c6ef372fe94f82bL , 0xa54ff53a5f1d36f1L ,
108+ 0x510e527fade682d1L , 0x9b05688c2b3e6c1fL , 0x1f83d9abfb41bd6bL , 0x5be0cd19137e2179L
105109 };
106110
107111 long [] finalHash = digest (data , initialHash );
@@ -119,6 +123,7 @@ public static String SHA512(byte[] data) {
119123 * <p>This method is wrapped by SHA224() and SHA256(). Both algorithms differ
120124 * only in two points: the initialization hashes are different and for SHA-224
121125 * the raw message digest is truncated by 1 byte.</p>
126+ *
122127 * @param data the data/message to be digested
123128 * @param hash the initial hash value, which in the process gets used
124129 * for the intermediate hashes
@@ -151,6 +156,7 @@ private static int[] digest(byte[] data, int[] hash) {
151156 * <p>This method is wrapped by SHA384() and SHA512(). Both algorithms differ
152157 * only in two points: the initialization hashes are different and for SHA-384
153158 * the raw message digest is truncated by 2 byte.</p>
159+ *
154160 * @param data the data/message to be digested
155161 * @param hash the initial hash value, which in the process gets used
156162 * for the intermediate hashes
@@ -180,7 +186,8 @@ private static long[] digest(byte[] data, long[] hash) {
180186
181187 /**
182188 * <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
184191 * @param blockSize the size of a data block (64 or 128 byte)
185192 * @return the padding for the data
186193 * @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) {
189196 byte [] padding ;
190197 int lastBlockLength = data .length % blockSize ;
191198 if (lastBlockLength + 1 > (blockSize / 8 ) * 7 ) {
192- padding = new byte [blockSize * 2 - lastBlockLength ];
199+ padding = new byte [blockSize * 2 - lastBlockLength ];
193200 } else {
194201 padding = new byte [blockSize - lastBlockLength ];
195202 }
@@ -206,8 +213,9 @@ private static byte[] pad(byte[] data, int blockSize) {
206213
207214 /**
208215 * Scrambles data blocks in a deterministic way.
216+ *
209217 * @param dataBlock the data blocks to be scrambled
210- * @param hash the resulting hash
218+ * @param hash the resulting hash
211219 * @see <a href="https://tools.ietf.org/html/rfc6234#section-6.2">SHA-224 and SHA-256 Processing</a>
212220 */
213221 private static void hashBlock (int [] dataBlock , int [] hash ) {
@@ -221,7 +229,7 @@ private static void hashBlock(int[] dataBlock, int[] hash) {
221229 W [i ] = dataBlock [i ];
222230 }
223231 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 ];
225233 }
226234
227235 // Initialize the working variables
@@ -251,8 +259,9 @@ private static void hashBlock(int[] dataBlock, int[] hash) {
251259
252260 /**
253261 * Scrambles data blocks in a deterministic way.
262+ *
254263 * @param dataBlock the data blocks to be scrambled
255- * @param hash the resulting hash
264+ * @param hash the resulting hash
256265 * @see <a href="https://tools.ietf.org/html/rfc6234#section-6.4">SHA-384 and SHA-512 Processing</a>
257266 */
258267 private static void hashBlock (long [] dataBlock , long [] hash ) {
@@ -266,7 +275,7 @@ private static void hashBlock(long[] dataBlock, long[] hash) {
266275 W [i ] = dataBlock [i ];
267276 }
268277 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 ];
270279 }
271280
272281 // Initialize the working variables
@@ -396,4 +405,4 @@ private static int ROTR(int x, int n) {
396405 private static long ROTR (long x , long n ) {
397406 return (x >>> n ) | (x << (64 - n ));
398407 }
399- }
408+ }
0 commit comments