Ejemplo n.º 1
0
        private void ResetTest()
        {
            // 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
            Blake2bDigest digest = new Blake2bDigest(key);

            digest.BlockUpdate(input, 0, input.Length);
            byte[] hash = new byte[digest.GetDigestSize()];
            digest.DoFinal(hash, 0);
            // Using a second instance, hash the input without calling doFinal()
            Blake2bDigest digest1 = new Blake2bDigest(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 (!Arrays.AreEqual(hash, hash1))
            {
                Fail("state was not reset");
            }
        }
Ejemplo n.º 2
0
        public static byte[] Hash(byte[] input)
        {
            Blake2bDigest digest = new Blake2bDigest(256);

            digest.BlockUpdate(input, 0, input.Length);
            byte[] hash = new byte[digest.GetDigestSize()];
            digest.DoFinal(hash, 0);
            return(hash);
        }
Ejemplo n.º 3
0
        public static byte[] Hash(byte[] bytes, int bitLength)
        {
            var digest = new Blake2bDigest(bitLength);
            var output = new byte[digest.GetDigestSize()];

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

            return(output);
        }
Ejemplo n.º 4
0
        public static byte[] GetDigest(byte[] msg, int size)
        {
            var result = new byte[size / 8];
            var digest = new Blake2bDigest(size);

            digest.BlockUpdate(msg, 0, msg.Length);
            digest.DoFinal(result, 0);

            return(result);
        }
Ejemplo n.º 5
0
        public static byte[] Blake2b(byte[] data)
        {
            Blake2bDigest black2b = new Blake2bDigest(32 * 8);

            black2b.BlockUpdate(data, 0, data.Length);
            var hashed = new byte[black2b.GetDigestSize()];

            black2b.DoFinal(hashed, 0);
            return(hashed);
        }
Ejemplo n.º 6
0
        public static byte[] HashBlake2B(byte[] data)
        {
            Blake2bDigest h = new Blake2bDigest(null, 32, null, null);

            h.BlockUpdate(data, 0, data.Length);
            var hash = new byte[32];

            h.DoFinal(hash, 0);
            return(hash);
        }
Ejemplo n.º 7
0
        public uint256 GetHash()
        {
            var blake2b = new Blake2bDigest(null, 32, null, _personalization);
            var hash    = new byte[blake2b.GetDigestSize()];

            blake2b.BlockUpdate(((MemoryStream)Inner).ToArrayEfficient(), 0, (int)Inner.Length);
            blake2b.DoFinal(hash, 0);

            return(new uint256(hash));
        }
Ejemplo n.º 8
0
        public override byte[] ComputeHash(byte[] input, int offset, int count)
        {
            var blake2b = new Blake2bDigest(DigestSize);
            var result  = new byte[blake2b.GetDigestSize()];

            blake2b.BlockUpdate(input, offset, count);
            blake2b.DoFinal(result, 0);

            return(result);
        }
Ejemplo n.º 9
0
        public override void PerformTest()
        {
            // test keyed test vectors:

            Blake2bDigest blake2bkeyed = new Blake2bDigest(Hex.Decode(keyedTestVectors[0][1]));

            for (int tv = 0; tv < keyedTestVectors.Length; tv++)
            {
                byte[] input = Hex.Decode(keyedTestVectors[tv][0]);
                blake2bkeyed.Reset();

                blake2bkeyed.BlockUpdate(input, 0, input.Length);
                byte[] keyedHash = new byte[64];
                blake2bkeyed.DoFinal(keyedHash, 0);

                if (!Arrays.AreEqual(Hex.Decode(keyedTestVectors[tv][2]), keyedHash))
                {
                    Fail("BLAKE2b mismatch on test vector ",
                         keyedTestVectors[tv][2],
                         Hex.ToHexString(keyedHash));
                }

                offsetTest(blake2bkeyed, input, keyedHash);
            }

            Blake2bDigest blake2bunkeyed = new Blake2bDigest();

            // test unkeyed test vectors:
            for (int i = 0; i < unkeyedTestVectors.Length; i++)
            {
                // blake2bunkeyed.update(
                // unkeyedTestVectors[i][1].getBytes("UTF-8"));
                // test update(byte b)
                byte[] unkeyedInput = Encoding.UTF8.GetBytes(unkeyedTestVectors[i][1]);
                for (int j = 0; j < unkeyedInput.Length; j++)
                {
                    blake2bunkeyed.Update(unkeyedInput[j]);
                }

                byte[] unkeyedHash = new byte[64];
                blake2bunkeyed.DoFinal(unkeyedHash, 0);
                blake2bunkeyed.Reset();

                if (!Arrays.AreEqual(Hex.Decode(unkeyedTestVectors[i][0]),
                                     unkeyedHash))
                {
                    Fail("BLAKE2b mismatch on test vector ",
                         unkeyedTestVectors[i][0],
                         Hex.ToHexString(unkeyedHash));
                }
            }

            cloneTest();
            resetTest();
        }
Ejemplo n.º 10
0
        protected static byte[] ComputeHash(byte[] message, int bits)
        {
            var digest = new Blake2bDigest(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);
        }
Ejemplo n.º 11
0
        public static byte[] Compute(byte[] input, int offset, int count, int digestSize)
        {
            var blake2b = new Blake2bDigest(digestSize);

            var result = new byte[blake2b.GetDigestSize()];

            blake2b.BlockUpdate(input, offset, count);
            blake2b.DoFinal(result, 0);

            return(result);
        }
Ejemplo n.º 12
0
        public override void PerformTest()
        {
            // test keyed test vectors:

            Blake2bDigest blake2bkeyed = new Blake2bDigest(Hex.Decode(keyedTestVectors[0, 1]));

            for (int tv = 0; tv < keyedTestVectors.GetLength(0); tv++)
            {
                byte[] input = Hex.Decode(keyedTestVectors[tv, 0]);
                blake2bkeyed.Reset();

                blake2bkeyed.BlockUpdate(input, 0, input.Length);
                byte[] keyedHash = new byte[64];
                blake2bkeyed.DoFinal(keyedHash, 0);

                if (!Arrays.AreEqual(Hex.Decode(keyedTestVectors[tv, 2]), keyedHash))
                {
                    Fail("BLAKE2b mismatch on test vector ", keyedTestVectors[tv, 2], Hex.ToHexString(keyedHash));
                }

                offsetTest(blake2bkeyed, input, keyedHash);
            }

            Blake2bDigest blake2bunkeyed = new Blake2bDigest();

            // test unkeyed test vectors:
            for (int i = 0; i < unkeyedTestVectors.GetLength(0); i++)
            {
                // test update(byte b)
                byte[] unkeyedInput = Encoding.UTF8.GetBytes(unkeyedTestVectors[i, 1]);
                for (int j = 0; j < unkeyedInput.Length; j++)
                {
                    blake2bunkeyed.Update(unkeyedInput[j]);
                }

                byte[] unkeyedHash = new byte[64];
                blake2bunkeyed.DoFinal(unkeyedHash, 0);
                blake2bunkeyed.Reset();

                if (!Arrays.AreEqual(Hex.Decode(unkeyedTestVectors[i, 0]), unkeyedHash))
                {
                    Fail("BLAKE2b mismatch on test vector ", unkeyedTestVectors[i, 0], Hex.ToHexString(unkeyedHash));
                }
            }

            CloneTest();
            ResetTest();
            DoTestNullKeyVsUnkeyed();
            DoTestLengthConstruction();
        }
Ejemplo n.º 13
0
        public static byte[] Blake2b(byte[] message)
        {
            /**
             * Thor public blockchain is using 256 bits digest
             */
            var blake2b = new Blake2bDigest(256);

            blake2b.BlockUpdate(message, 0, message.Length);
            var digest = new byte[32];
            int size   = blake2b.DoFinal(digest, 0);

            if (size > 0)
            {
                return(digest);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 14
0
        private void DoTestNullKeyVsUnkeyed()
        {
            byte[] abc = Strings.ToByteArray("abc");

            for (int i = 1; i != 64; i++)
            {
                Blake2bDigest dig1 = new Blake2bDigest(i * 8);
                Blake2bDigest dig2 = new Blake2bDigest(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));
            }
        }
Ejemplo n.º 15
0
        private void CheckClone(Blake2bDigest blake2bCloneSource, byte[] expected)
        {
            byte[] message = Hex.Decode(keyedTestVectors[3, 0]);

            blake2bCloneSource.BlockUpdate(message, 0, message.Length);

            byte[] hash = new byte[blake2bCloneSource.GetDigestSize()];

            Blake2bDigest digClone = new Blake2bDigest(blake2bCloneSource);

            blake2bCloneSource.DoFinal(hash, 0);
            if (!AreEqual(expected, hash))
            {
                Fail("clone source not correct");
            }

            digClone.DoFinal(hash, 0);
            if (!AreEqual(expected, hash))
            {
                Fail("clone not correct");
            }
        }