예제 #1
0
        public void DoTestDigestWithKeyedTestVectors()
        {
            Blake2sDigest digest = new Blake2sDigest(Hex.Decode(keyedTestVectors[0, 1]));

            for (int i = 0; i != keyedTestVectors.GetLength(0); i++)
            {
                byte[] input = Hex.Decode(keyedTestVectors[i, 0]);
                digest.Reset();

                digest.BlockUpdate(input, 0, input.Length);
                byte[] hash = new byte[32];
                digest.DoFinal(hash, 0);

                if (!AreEqual(Hex.Decode(keyedTestVectors[i, 2]), hash))
                {
                    Fail("BLAKE2s mismatch on test vector ", keyedTestVectors[i, 2], Hex.ToHexString(hash));
                }
            }
        }
예제 #2
0
        public void DoTestDigestWithKeyedTestVectorsAndRandomUpdate()
        {
            Blake2sDigest digest = new Blake2sDigest(Hex.Decode(
                                                         keyedTestVectors[0][1]));
            Random random = new Random();

            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j != keyedTestVectors.Length; j++)
                {
                    String[] keyedTestVector = keyedTestVectors[j];
                    byte[]   input           = Hex.Decode(keyedTestVector[0]);
                    if (input.Length < 3)
                    {
                        continue;
                    }
                    digest.Reset();

                    int pos = (random.Next() & 0xffff) % input.Length;
                    if (pos > 0)
                    {
                        digest.BlockUpdate(input, 0, pos);
                    }
                    digest.Update(input[pos]);
                    if (pos < (input.Length - 1))
                    {
                        digest.BlockUpdate(input, pos + 1, input.Length - (pos + 1));
                    }

                    byte[] hash = new byte[32];
                    digest.DoFinal(hash, 0);

                    if (!AreEqual(Hex.Decode(keyedTestVector[2]), hash))
                    {
                        Fail("BLAKE2s mismatch on test vector ",
                             keyedTestVector[2],
                             Hex.ToHexString(hash));
                    }
                }
            }
        }
예제 #3
0
        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));
            }
        }