LAB-9
Gokul Krishna k
21BAI1012
SHA512
Code
public class SHA512 { // Constants for
SHA-512 private static final long[] K =
{
0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL,
0xe9b5dba58189dbbcL,
0x3956c25bf348b538L, 0x59f111f1b605d019L, 0x923f82a4af194f9bL,
0xab1c5ed5da6d8118L,
0xd807aa98a3030242L, 0x12835b014f3b9c6bL, 0x243185be4ee4b28cL,
0x550c7dc3d5ffb4e2L,
0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c1c65cL,
0xc19bf174cf692694L,
0xe49b69c19e8b11ffL, 0xefbe4786384f25e3L, 0x0fc19dc68b8cd5b5L,
0x240ca1cc77ac9c65L,
0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L,
0x76f988da831153b5L,
0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL,
0xbf597fc7beef0ee4L,
0xc6e00bf33da88fc2L, 0xd5a79147930aa725L, 0x06ca6351e003826fL,
0x142929670a0e6e70L,
0x27b70a85c6ca6351L, 0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL,
0x53380d139d95b3dfL,
0x650a73548baf63deL, 0x766a0abb3c6ef372L, 0x81c2c92e47edaee6L,
0x92722c851482353bL,
0xa2bfe8a14cf10364L, 0xa81a664bcbc9b3b1L, 0xc24b8b70d0f89791L,
0xc76c51a30654be30L,
0xd192e819d6ef5218L, 0xd69906245565a910L, 0xf40e35855771202aL,
0x106aa07032bbd1b8L,
0x19b4e1c670f2e5eaL, 0x1e376c085141ab53L, 0x2748774c1e4e2b6cL,
0x34b0bcb5bbee57abL,
0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL, 0x5b9cca4f7763e373L,
0x682e6ff3d6b2b8a3L,
0x748f82ee5defb2fcL, 0x78a5636f43172f60L, 0x84c87814a1f0ab72L,
0x8cc702081a6439ecL,
0x90befffa23631e28L, 0xa4506ceb0bfe19b8L, 0xbef9a3f7b2c67915L,
0xc67178f2e372532bL,
0xca273eceea26619cL, 0xd186b8c721c0c207L, 0xeada7dd6cde0eb1eL,
0xf57d4f7fee6ed178L,
0x06f067aa721f054fL, 0x0a637b1aeaa3aab1L, 0x113f9804be0fb4e1L,
0x1b710b35131c471bL,
0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL,
0x431d67c49c100d4cL,
0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL, 0x5fcb6fab3ad6fa3eL,
0x6c44198c4a475817L
};
// Initial hash values for SHA-512 private static
final long[] H = {
0x6a09e667f3bcc908L, 0xbb67ae8584caa73bL, 0x3c6ef372fe94f82bL,
0xa54ff53a5f1d36f1L,
0x510e527fade682d1L, 0x9b05688c2b3e6c1fL, 0x1f83d9abfb41bd6bL, 0x5be0cd19137e2179L
};
// The main SHA-512 hash method public static byte[] hash(byte[] message) { // Pre-processing
(padding the message) int originalByteLen = message.length; long originalBitLen = originalByteLen * 8;
int paddingLength = (896 - (originalByteLen * 8) % 1024 + 1024) % 1024; int totalLength =
originalByteLen + paddingLength / 8 + 16; // Total length after
padding
byte[] paddedMessage = new byte[totalLength];
System.arraycopy(message, 0, paddedMessage, 0, originalByteLen); paddedMessage[originalByteLen] =
(byte) 0x80; // Append the bit '1'
// Append the original length in bits as a 128-bit big-endian integer for (int i = 0; i < 16; i++) {
paddedMessage[totalLength - 16 + i] = (byte) (originalBitLen >>> (8 * (15 - i)));
}
// Initialize working variables long[]
hash = H.clone(); long[] W = new
long[80];
// Process the message in 1024-bit chunks
for (int i = 0; i < paddedMessage.length / 128; i++) {
// Prepare the message schedule W
for (int t = 0; t < 16; t++) {
W[t] = ((paddedMessage[i * 128 + t * 8] & 0xffL) << 56)
| ((paddedMessage[i * 128 + t * 8 + 1] & 0xffL) << 48) |
((paddedMessage[i * 128 + t * 8 + 2] & 0xffL) << 40) |
((paddedMessage[i * 128 + t * 8 + 3] & 0xffL) << 32) |
((paddedMessage[i * 128 + t * 8 + 4] & 0xffL) << 24)
| ((paddedMessage[i * 128 + t * 8 + 5] & 0xffL) << 16)
| ((paddedMessage[i * 128 + t * 8 + 6] & 0xffL) << 8) |
(paddedMessage[i * 128 + t * 8 + 7] & 0xffL);
}
// Extend the first 16 words into the remaining 64 words of the array W for (int t = 16; t < 80; t++) {
long s0 = Long.rotateRight(W[t - 15], 1) ^ Long.rotateRight(W[t - 15], 8) ^ (W[t
- 15] >>> 7); long s1 = Long.rotateRight(W[t - 2], 19) ^ Long.rotateRight(W[t - 2], 61) ^ (W[t
- 2] >>> 6);
W[t] = (W[t - 16] + s0 + W[t - 7] + s1) & 0xffffffffffffffffL;
}
// Initialize the working variables to current hash value long a =
hash[0]; long b = hash[1]; long c = hash[2]; long d = hash[3]; long e
= hash[4]; long f = hash[5]; long g = hash[6]; long h = hash[7];
// Compression function main loop for (int t = 0; t < 80; t++) { long S1 =
Long.rotateRight(e, 14) ^ Long.rotateRight(e, 18) ^
Long.rotateRight(e, 41); long ch = (e & f) ^ (~e & g); long temp1 = (h + S1 + ch + K[t] +
W[t]) & 0xffffffffffffffffL; long S0 = Long.rotateRight(a, 28) ^
Long.rotateRight(a, 34) ^
Long.rotateRight(a, 39); long maj = (a & b) ^ (a & c) ^ (b & c); long
temp2 = (S0 + maj) & 0xffffffffffffffffL;
h = g; g
= f; f =
e;
e = (d + temp1) & 0xffffffffffffffffL; d = c; c = b; b =
a; a = (temp1 + temp2) & 0xffffffffffffffffL;
// Print intermediate results for round 0 if (t == 0)
{
System.out.printf("Intermediate results at round %d: a=%016x b=%016x c=
%016x d=%016x e=%016x f=%016x g=%016x h=%016x\n",
t, a, b, c, d, e, f, g, h);
}
}
// Add the compressed chunk to the current hash value
hash[0] = (hash[0] + a) & 0xffffffffffffffffL; hash[1] = (hash[1]
+ b) & 0xffffffffffffffffL; hash[2] = (hash[2] + c) &
0xffffffffffffffffL; hash[3] = (hash[3] + d) & 0xffffffffffffffffL;
hash[4] = (hash[4] + e) & 0xffffffffffffffffL; hash[5] = (hash[5]
+ f) & 0xffffffffffffffffL; hash[6] = (hash[6] + g) &
0xffffffffffffffffL; hash[7] = (hash[7] + h) & 0xffffffffffffffffL;
}
// Produce the final hash value (big-endian) as a byte array
byte[] hashBytes = new byte[64]; for (int i = 0; i < hash.length; i++) { for
(int j = 0; j < 8; j++) { hashBytes[i * 8 + (7 - j)] = (byte) (hash[i] >>> (j * 8));
}
}
return hashBytes;
}
// Utility method to convert byte array to hex string public static
String bytesToHex(byte[] bytes) { StringBuilder hexString = new
StringBuilder(); for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b); if
(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
// Main method for testing
public static void main(String[] args) {
// Test case with message length less than 896 bits
String testInput1 = "abc"; byte[] hash1 =
hash(testInput1.getBytes());
System.out.println("SHA-512 hash of \"" + testInput1 + "\": " + bytesToHex(hash1));
// Test case with message length equal to 896 bits (112 bytes) String testInput2 =
"a".repeat(112); // 112 bytes = 896 bits byte[] hash2 =
hash(testInput2.getBytes());
System.out.println("SHA-512 hash of \"" + "a of 112 times" + "\": " + bytesToHex(hash2));
}
}
Ouput [screenshot]