Esempio n. 1
0
        static byte[] Sha512Algorithm(byte[] plaintext, Word64[] H0, int numberBits)
        {
            Block1024[] blocks = ConvertPaddedMessageToBlock1024Array(PadPlainText1024(plaintext));

            // Define the hash variable and set its initial values.
            Word64[] H = H0;

            for (int i = 0; i < blocks.Length; i++)
            {
                Word64[] W = CreateMessageScheduleSha512(blocks[i]);

                // Set the working variables a,...,h to the current hash values.
                Word64 a = H[0];
                Word64 b = H[1];
                Word64 c = H[2];
                Word64 d = H[3];
                Word64 e = H[4];
                Word64 f = H[5];
                Word64 g = H[6];
                Word64 h = H[7];

                for (int t = 0; t < 80; t++)
                {
                    Word64 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[t] + W[t];
                    Word64 T2 = Sigma0_512(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 Word64 Hash Values
            byte[] hash = ShaUtilities.Word64ArrayToByteArray(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);
        }
Esempio n. 2
0
        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));
        }
Esempio n. 3
0
        static Block1024[] ConvertPaddedMessageToBlock1024Array(byte[] M)
        {
            // We are assuming M is padded, so the number of bits in M is divisible by 1024
            int numberBlocks = (M.Length * 8) / 1024;  // same as: M.Length / 128

            Block1024[] blocks = new Block1024[numberBlocks];

            for (int i = 0; i < numberBlocks; i++)
            {
                // First extract the relavant subarray from M
                byte[] B = new byte[128]; // 128 * 8 = 1024

                for (int j = 0; j < 128; j++)
                {
                    B[j] = M[i * 128 + j];
                }

                Word64[] words = ShaUtilities.ByteArrayToWord64Array(B);
                blocks[i] = new Block1024(words);
            }

            return(blocks);
        }
Esempio n. 4
0
        static Block512[] ConvertPaddedTextToBlock512Array(byte[] paddedtext)
        {
            // We are assuming M has been padded, so the number of bits in M is divisible by 512
            int numberBlocks = (paddedtext.Length * 8) / 512;  // same as: paddedtext.Length / 64

            Block512[] blocks = new Block512[numberBlocks];

            for (int i = 0; i < numberBlocks; i++)
            {
                // First extract the relavant subarray from paddedtext
                byte[] B = new byte[64]; // 64 * 8 = 512

                for (int j = 0; j < 64; j++)
                {
                    B[j] = paddedtext[i * 64 + j];
                }

                Word32[] words = ShaUtilities.ByteArrayToWord32Array(B);
                blocks[i] = new Block512(words);
            }

            return(blocks);
        }
Esempio n. 5
0
 public static string Sha384(string plaintext)
 {
     return(ShaUtilities.ByteArrayToHexString(Sha384(ShaUtilities.StringToByteArray(plaintext))));
 }