Exemple #1
0
        public static byte[] Hash(byte[] bytes, int bitLength)
        {
            var digest = new Blake2sDigest(bitLength);
            var output = new byte[digest.GetDigestSize()];

            digest.BlockUpdate(bytes, 0, bytes.Length);
            digest.DoFinal(output, 0);

            return(output);
        }
Exemple #2
0
        protected static byte[] ComputeHash(byte[] message, int bits)
        {
            var digest = new Blake2sDigest(bits);
            var output = new byte[digest.GetDigestSize()];

            byte[] msg = message ?? new byte[0];
            digest.BlockUpdate(msg, 0, msg.Length);
            digest.DoFinal(output, 0);

            return(output);
        }
        public void DoTestReset()
        {
            // Generate a non-zero key
            byte[] key = new byte[32];
            for (byte i = 0; i < key.Length; i++)
            {
                key[i] = i;
            }
            // Generate some non-zero input longer than the key
            byte[] input = new byte[key.Length + 1];
            for (byte i = 0; i < input.Length; i++)
            {
                input[i] = i;
            }
            // Hash the input
            Blake2sDigest digest = new Blake2sDigest(key);

            digest.BlockUpdate(input, 0, input.Length);
            byte[] hash = new byte[digest.GetDigestSize()];
            digest.DoFinal(hash, 0);
            // Create a second instance, hash the input without calling doFinal()
            Blake2sDigest digest1 = new Blake2sDigest(key);

            digest1.BlockUpdate(input, 0, input.Length);
            // Reset the second instance and hash the input again
            digest1.Reset();
            digest1.BlockUpdate(input, 0, input.Length);
            byte[] hash1 = new byte[digest.GetDigestSize()];
            digest1.DoFinal(hash1, 0);
            // The hashes should be identical
            if (!AreEqual(hash, hash1))
            {
                Fail("BLAKE2s mismatch on test vector ",
                     Hex.ToHexString(hash),
                     Hex.ToHexString(hash1));
            }
        }