static byte[] Sha256Algorithm(byte[] plaintext, Word32[] H0, int numberBits) { Block512[] blocks = ConvertPaddedTextToBlock512Array(PadPlainText512(plaintext)); // Define the hash variables and set their initial values. Word32[] H = H0; for (int i = 0; i < blocks.Length; i++) { Word32[] W = CreateMessageScheduleSha256(blocks[i]); // Set the working variables a,...,h to the current hash values. Word32 a = H[0]; Word32 b = H[1]; Word32 c = H[2]; Word32 d = H[3]; Word32 e = H[4]; Word32 f = H[5]; Word32 g = H[6]; Word32 h = H[7]; for (int t = 0; t < 64; t++) { Word32 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[t] + W[t]; Word32 T2 = Sigma0_256(a) + Maj(a, b, c); h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; } // Update the current value of the hash H after processing block i. H[0] += a; H[1] += b; H[2] += c; H[3] += d; H[4] += e; H[5] += f; H[6] += g; H[7] += h; } // Concatenate all the Word32 Hash Values byte[] hash = ShaUtilities.Word32ArrayToByteArray(H); // The number of bytes in the final output hash int numberBytes = numberBits / 8; byte[] truncatedHash = new byte[numberBytes]; Array.Copy(hash, truncatedHash, numberBytes); return(truncatedHash); }
static byte[] Sha1Algorithm(byte[] plaintext) { Block512[] blocks = ConvertPaddedTextToBlock512Array(PadPlainText512(plaintext)); // Define the hash variable and set its initial values. Word32[] H = new Word32[5]; H0Sha1.CopyTo(H, 0); for (int i = 0; i < blocks.Length; i++) { Word32[] W = CreateMessageScheduleSha1(blocks[i]); // Set the working variables a,...,e to the current hash values. Word32 a = H[0]; Word32 b = H[1]; Word32 c = H[2]; Word32 d = H[3]; Word32 e = H[4]; for (int t = 0; t < 80; t++) { Word32 T = RotL(5, a) + f(t, b, c, d) + e + K1[t] + W[t]; e = d; d = c; c = RotL(30, b); b = a; a = T; } // Update the current value of the hash H after processing block i. H[0] += a; H[1] += b; H[2] += c; H[3] += d; H[4] += e; } // Concatenating the final 5 hash words H[0],...,H[4] gives the digest. // Since each H[i] is 4 bytes, the digest is 5 * 4 = 20 bytes = 160 bits. return(ShaUtilities.Word32ArrayToByteArray(H)); }