Ejemplo n.º 1
0
        public void Sha256()
        {
            const string input    = "sha256 input test";
            const string expected = "d7ecf2c1caa1c5120a2d90ec7411ba6d26cb83f02fafadcef90bcad201d5b37e";
            var          actual   = SHA.ComputeSHA256(input.ToBytes()).ToHex();

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 2
0
        public void Test_SHA1_SHA256()
        {
            var random   = new System.Random(123);
            var bytes512 = new byte[512];

            random.NextBytes(bytes512);


            var output = new byte[20];


            var sha1managed = new SHA1Managed();

            byte[] tmp1 = null;
            uint[] tmp2 = null;


            for (int dataLength = 0; dataLength < bytes512.Length; dataLength++)
            {
                SHA.ComputeSHA1(bytes512, dataLength, output, 0, ref tmp1, ref tmp2);
                Assert.AreEqual(sha1managed.ComputeHash(new ByteBuffer(bytes512, 0, dataLength).ToArray()), output);
            }

            output = new byte[32];

            var sha256managed = new SHA256Managed();

            for (int dataLength = 0; dataLength < bytes512.Length; dataLength++)
            {
                SHA.ComputeSHA256(bytes512, dataLength, output, 0, ref tmp1, ref tmp2);
                Assert.AreEqual(sha256managed.ComputeHash(new ByteBuffer(bytes512, 0, dataLength).ToArray()), output);
            }

            var key = new byte[64];

            random.NextBytes(key);

            output = new byte[20];

            for (int keyLength = 0; keyLength < key.Length; keyLength++)
            {
                for (int dataLength = 0; dataLength < bytes512.Length; dataLength++)
                {
                    HMAC_SHA1.ComputeHmacSha1(new ByteBuffer(key, 0, keyLength), new ByteBuffer(bytes512, 0, dataLength), output, 0);
                    Assert.AreEqual(new HMACSHA1(new ByteBuffer(key, 0, keyLength).ToArray()).ComputeHash(bytes512, 0, dataLength), output);
                }
            }


            HMAC_SHA1.ComputeHmacSha1(new ByteBuffer(key, 0, key.Length), new ByteBuffer(bytes512, 40, 200), output, 0);
            Assert.AreEqual(new HMACSHA1(key).ComputeHash(bytes512, 40, 200), output);
        }