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); }
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 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)); } } }
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)); } } } }
public void RunSelfTest() { Blake2sDigest testDigest = new Blake2sDigest(); byte[] md = new byte[32]; for (int i = 0; i < 4; i++) { int outlen = SELF_TEST_DIGEST_LEN[i]; for (int j = 0; j < 6; j++) { int inlen = SELF_TEST_INPUT_LEN[j]; // unkeyed hash byte[] input = selfTestSequence(inlen, inlen); Blake2sDigest unkeyedDigest = new Blake2sDigest(outlen * 8); unkeyedDigest.BlockUpdate(input, 0, inlen); unkeyedDigest.DoFinal(md, 0); // hash the hash testDigest.BlockUpdate(md, 0, outlen); // keyed hash byte[] key = selfTestSequence(outlen, outlen); Blake2sDigest keyedDigest = new Blake2sDigest(key, outlen, null, null); keyedDigest.BlockUpdate(input, 0, inlen); keyedDigest.DoFinal(md, 0); // hash the hash testDigest.BlockUpdate(md, 0, outlen); } } byte[] hash = new byte[32]; testDigest.DoFinal(hash, 0); if (!AreEqual(Hex.Decode(SELF_TEST_RESULT), hash)) { Fail("BLAKE2s mismatch on test vector ", SELF_TEST_RESULT, Hex.ToHexString(hash)); } }
private void DoTestNullKeyVsUnkeyed() { byte[] abc = Strings.ToByteArray("abc"); for (int i = 1; i != 32; i++) { Blake2sDigest dig1 = new Blake2sDigest(i * 8); Blake2sDigest dig2 = new Blake2sDigest(null, i, null, null); byte[] out1 = new byte[i]; byte[] out2 = new byte[i]; dig1.BlockUpdate(abc, 0, abc.Length); dig2.BlockUpdate(abc, 0, abc.Length); dig1.DoFinal(out1, 0); dig2.DoFinal(out2, 0); IsTrue(Arrays.AreEqual(out1, out2)); } }
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)); } }