Пример #1
0
        public void ComputeHash_ReuseTest()
        {
            byte[] msg1 = Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog");
            byte[] msg2 = Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy cog");
            byte[] exp1 = Helper.HexToBytes("07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6");
            byte[] exp2 = Helper.HexToBytes("3eeee1d0e11733ef152a6c29503b3ae20c4f1f3cda4cb26f1bc1a41f91c7fe4ab3bd86494049e201c4bd5155f31ecb7a3c8606843c4cc8dfcab7da11c8ae5045");

            using Sha512 sha = new Sha512();
            byte[] act1 = sha.ComputeHash(msg1);
            byte[] act2 = sha.ComputeHash(msg2);

            Assert.Equal(exp1, act1);
            Assert.Equal(exp2, act2);
        }
Пример #2
0
        public void ComputeHash_NistMonteCarloTest()
        {
            byte[]  seed  = Helper.HexToBytes("5c337de5caf35d18ed90b5cddfce001ca1b8ee8602f367e7c24ccca6f893802fb1aca7a3dae32dcd60800a59959bc540d63237876b799229ae71a2526fbc52cd");
            JObject jObjs = Helper.ReadResource <JObject>("Sha512NistTestData");
            int     size  = 64;

            byte[] toHash = new byte[3 * size];

            byte[] M0 = seed;
            byte[] M1 = seed;
            byte[] M2 = seed;

            using Sha512 sha = new Sha512(false);

            foreach (var item in jObjs["MonteCarlo"])
            {
                byte[] expected = Helper.HexToBytes(item.ToString());
                for (int i = 0; i < 1000; i++)
                {
                    Buffer.BlockCopy(M0, 0, toHash, 0, size);
                    Buffer.BlockCopy(M1, 0, toHash, size, size);
                    Buffer.BlockCopy(M2, 0, toHash, size * 2, size);

                    M0 = M1;
                    M1 = M2;
                    M2 = sha.ComputeHash(toHash);
                }
                M0 = M2;
                M1 = M2;

                Assert.Equal(expected, M2);
            }
        }
Пример #3
0
 public void ComputeHashTwiceTest(byte[] message, byte[] expectedHash)
 {
     using Sha512 sha = new Sha512();
     byte[] actualHash = sha.ComputeHashTwice(message);
     expectedHash = sha.ComputeHash(expectedHash);
     Assert.Equal(expectedHash, actualHash);
 }
Пример #4
0
        public void ComputeHash_AMillionATest()
        {
            using Sha512 sha = new Sha512();
            byte[] actualHash   = sha.ComputeHash(HashTestCaseHelper.GetAMillionA());
            byte[] expectedHash = Helper.HexToBytes("e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b");

            Assert.Equal(expectedHash, actualHash);
        }
Пример #5
0
        public void ComputeHash_WithIndexTest()
        {
            using Sha512 sha = new Sha512();
            byte[] data         = Helper.HexToBytes("123fab54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67f3a25c92");
            byte[] actualHash   = sha.ComputeHash(data, 3, 43);
            byte[] expectedHash = Helper.HexToBytes("07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6");

            Assert.Equal(expectedHash, actualHash);
        }
Пример #6
0
        public void TestSha512()
        {
            var sha512 = new Sha512();
            var bytes  = Encoding.UTF8.GetBytes("abc");

            var hash      = sha512.ComputeHash(bytes);
            var sha512str = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();

            Assert.AreEqual(sha512str, "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
        }
Пример #7
0
        public void ComputeHash_DoubleTest()
        {
            using Sha512 sha = new Sha512(true);
            var data = Helper.HexToBytes("fb8049137747e712628240cf6d7056ea2870170cb7d9bc713d91e901b514c6ae7d7dda3cd03ea1b99cf85046a505f3590541123d3f8f2c22c4d7d6e65de65c4ebb9251f09619");

            byte[] actualHash   = sha.ComputeHash(data);
            byte[] expectedHash = Helper.HexToBytes("00920ac1123d211929f0ef40d0ab3775abc987c606219301eb5995ff1053043a3c24906e88a74e4b2d6e1f6aa830a4f8b7e5e6edb7d090d37033abe45153a8e2");

            Assert.Equal(expectedHash, actualHash);
        }
Пример #8
0
        public void Hash(int count, string value)
        {
            Assert.Equal(count, value.Length);
            var shaClr       = SHA512.Create();
            var sha          = new Sha512();
            var data         = Encoding.UTF8.GetBytes(value);
            var expectedHash = shaClr.ComputeHash(data);

            var result = new byte[64];

            sha.ComputeHash(data, result);
            Assert.Equal(expectedHash, result);
        }
Пример #9
0
        /// <summary>
        /// Encrypts a message with this public key using Elliptic Curve Integrated Encryption Scheme (ECIES)
        /// with AES-128-CBC as cipher and HMAC-SHA256 as MAC.
        /// </summary>
        /// <exception cref="ArgumentNullException"/>
        /// <param name="message">Message to encrypt</param>
        /// <param name="magic">
        /// [Default value = BIE1]
        /// A magic string added to encrypted result before computing its HMAC-SHA256
        /// </param>
        /// <returns>Encrypted result as a base-64 encoded string</returns>
        public string Encrypt(string message, string magic = "BIE1")
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message), "Mesage can not be null.");
            }
            if (magic is null)
            {
                throw new ArgumentNullException(nameof(magic), "Magic can not be null.");
            }

            byte[] magicBytes = Encoding.UTF8.GetBytes(magic);

            // TODO: investigate if this can become deterministic (it can't be based on message or pubkey
            //       otherwise the ephemeral key is revealed)
            using SharpRandom rng      = new SharpRandom();
            using PrivateKey ephemeral = new PrivateKey(rng);

            byte[] ecdhKey = new PublicKey(calc.Multiply(ephemeral.ToBigInt(), point)).ToByteArray(true);
            using Sha512 sha512 = new Sha512();
            byte[] key = sha512.ComputeHash(ecdhKey);

            using Aes aes = new AesManaged
                  {
                      KeySize = 128,
                      Key     = key.SubArray(16, 16),
                      Mode    = CipherMode.CBC,
                      IV      = key.SubArray(0, 16),
                      Padding = PaddingMode.PKCS7
                  };
            using ICryptoTransform encryptor = aes.CreateEncryptor();
            using MemoryStream encStream     = new MemoryStream();
            using CryptoStream cryptStream   = new CryptoStream(encStream, encryptor, CryptoStreamMode.Write);
            using (StreamWriter swEncrypt = new StreamWriter(cryptStream))
            {
                swEncrypt.Write(message);
            }

            byte[] encrypted = magicBytes.ConcatFast(ephemeral.ToPublicKey().ToByteArray(true)).ConcatFast(encStream.ToArray());
            using HmacSha256 hmac = new HmacSha256();
            byte[] mac = hmac.ComputeHash(encrypted, key.SubArray(32));

            return(encrypted.ConcatFast(mac).ToBase64());
        }
Пример #10
0
        public void ComputeHash_ExceptionsTest()
        {
            byte[] goodBa = { 1, 2, 3 };
            Sha512 sha    = new Sha512();

            Assert.Throws <ArgumentNullException>(() => sha.ComputeHash(null));
            Assert.Throws <ArgumentNullException>(() => sha.ComputeHash(null, 0, 1));
            Assert.Throws <IndexOutOfRangeException>(() => sha.ComputeHash(goodBa, 0, 5));
            Assert.Throws <IndexOutOfRangeException>(() => sha.ComputeHash(goodBa, 10, 1));

            sha.Dispose();
            Assert.Throws <ObjectDisposedException>(() => sha.ComputeHash(goodBa));
            Assert.Throws <ObjectDisposedException>(() => sha.ComputeHash(goodBa, 0, 2));
        }
Пример #11
0
        static void Main(string[] args)
        {
            //HMACSHA256 _clrSha256 = new HMACSHA256();
            //Hmac _sha256 = new Hmac(new Sha256(), _clrSha256.Key);
            //byte[] _buffer = new byte[32];

            //var value = Encoding.UTF8.GetBytes("abcdefghijklmnopqrstuvwxyz012345678901234567890123456789abcdefghijklmnopqrstuvwxyz012345678901234567890123456789abcdefghijklmnopqrstuvwxyz012345678901234567890123456789");
            //while (true)
            //{
            //    var hash = _clrSha256.ComputeHash(value);
            //    _sha256.ComputeHash(value, _buffer);
            //    if (hash.Length > 32)
            //        break;
            //}

            var _clrSha256 = SHA512.Create();
            var sha256     = new Sha512();
            var sha256Simd = new Sha512Simd();

            //var sha256x = new Sha256X();
            //var sha256Struct = new Sha256Struct();
            //var sha256StructUnroll = new Sha256StructUnroll();
            byte[] _buffer    = new byte[64];
            byte[] _buffer_6  = new byte[64];
            byte[] _buffer_12 = new byte[64];

            var value = Encoding.UTF8.GetBytes("abcdefghijklmnopqrstuvwxyz01234567890123456789012345678901234567abcdefghijklmnopqrstuvwxyz01234567890123456789012345678901234567abcdefghijklmnopqrstuvwxyz01234567890123456789012345678901234567abcdefghijklmnopqrstuvwxyz01234567890123456789012345678901234567abcdefghijklmnopqrstuvwxyz01234567890123456789012345678901234567abcdefghijklmnopqrstuvwxyz01234567890123456789012345678901234567abcdefghijklmnopqrstuvwxyz01234567890123456789012345678901234567abcdefghijklmnopqrstuvwxyz01234567890123456789012345678901234567");

            while (true)
            {
                //sha256Struct.ComputeHash(value, _buffer_6);
                //var hash = _clrSha256.ComputeHash(value);
                sha256.ComputeHash(value, _buffer);
                sha256Simd.ComputeHash(value, _buffer);
                //sha256x.ComputeHash(value, _buffer);
                ////sha256StructUnroll.ComputeHash(value, _buffer_12);
                //if (hash.Length > 32)
                //break;
            }
        }
Пример #12
0
        protected override void ComputeHash(ReadOnlySpan <byte> source, Span <byte> destination)
        {
            var sha512 = new Sha512();

            sha512.ComputeHash(source, destination);
        }
Пример #13
0
 public void ComputeHash_NistShortTest(byte[] message, byte[] expected)
 {
     using Sha512 sha = new Sha512(false);
     byte[] actual = sha.ComputeHash(message);
     Assert.Equal(expected, actual);
 }
Пример #14
0
 public byte[] Library_Sha512(byte[] data) => libSha.ComputeHash(data);
Пример #15
0
 public byte[] Sha512_Optimized(byte[] value)
 {
     _sha512.ComputeHash(value, _buffer);
     return(_buffer);
 }