public void FIPS186_b(string testName, SHA512 hash, byte[] input, byte[] result)
 {
     byte[] output = hash.ComputeHash(input, 0, input.Length);
     Assert.AreEqual(result, output, testName + ".b.1");
     Assert.AreEqual(result, hash.Hash, testName + ".b.2");
     // required or next operation will still return old hash
     hash.Initialize();
 }
 public void FIPS186_d(string testName, SHA512 hash, byte[] input, byte[] result)
 {
     byte[] output = hash.TransformFinalBlock(input, 0, input.Length);
     // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
     // AssertEquals( testName + ".d.1", result, output );
     Assert.IsNotNull(output, testName + ".d.1");
     Assert.AreEqual(result, hash.Hash, testName + ".d.2");
     // required or next operation will still return old hash
     hash.Initialize();
 }
        public void FIPS186_c(string testName, SHA512 hash, byte[] input, byte[] result)
        {
            MemoryStream ms = new MemoryStream(input);

            byte[] output = hash.ComputeHash(ms);
            Assert.AreEqual(result, output, testName + ".c.1");
            Assert.AreEqual(result, hash.Hash, testName + ".c.2");
            // required or next operation will still return old hash
            hash.Initialize();
        }
 public void FIPS186_e(string testName, SHA512 hash, byte[] input, byte[] result)
 {
     byte[] copy = new byte [input.Length];
     for (int i = 0; i < input.Length - 1; i++)
     {
         hash.TransformBlock(input, i, 1, copy, i);
     }
     byte[] output = hash.TransformFinalBlock(input, input.Length - 1, 1);
     // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
     // AssertEquals (testName + ".e.1", result, output);
     Assert.IsNotNull(output, testName + ".e.1");
     Assert.AreEqual(result, hash.Hash, testName + ".e.2");
     // required or next operation will still return old hash
     hash.Initialize();
 }
Ejemplo n.º 5
0
 public override void Initialize()
 {
     hash.Initialize();
 }
Ejemplo n.º 6
0
 public Sha512()
 {
     _digest = SHA512.Create();
     _digest.Initialize();
 }
Ejemplo n.º 7
0
 /// <inheritdoc />
 public override void Initialize()
 {
     _hasher.Initialize();
 }
Ejemplo n.º 8
0
 public SHA512Hash()
 {
     _sha512 = SHA512.Create();
     _sha512.Initialize();
 }
 public override void Initialize()
 {
     _implementation.Initialize();
 }
Ejemplo n.º 10
0
        private byte[] DecryptBinaryDataInternal(ref byte[] data, ref byte[] encryptedKey, bool useExternalKey, out bool dataIsValid)
        {
            var msDecrypted = new MemoryStream();
            var msEncrypted = new MemoryStream(data, MetadataLength - 1, data.Length - MetadataLength);
            var msMetadata  = new MemoryStream(data, 0, MetadataLength);

            using (Aes aesAlg = AesCng.Create("AES"))
            {
                if (useExternalKey)
                {
                    ProtectedMemory.Unprotect(encryptedKey, MemoryProtectionScope.SameProcess);
                }

                if (aesAlg == null)
                {
                    dataIsValid = false;
                    return(null);
                }

                SHA512 hashAlg = SHA512Cng.Create("SHA512");
                hashAlg.Initialize();


                byte[] initVector = new byte[16];
                msMetadata.Read(initVector, 0, initVector.Length);

                byte[] hashBuffer = new byte[64];
                msMetadata.Read(hashBuffer, 0, hashBuffer.Length);

                byte[] entropyBytes = new byte[32];
                msMetadata.Read(entropyBytes, 0, entropyBytes.Length);


                int encryptedDataLength = data.Length;

                aesAlg.BlockSize = 128;
                aesAlg.KeySize   = 256;
                aesAlg.Padding   = PaddingMode.PKCS7;
                aesAlg.Mode      = CipherMode.CBC;
                aesAlg.IV        = initVector;
                var    rfc2898DeriveBytes = new Rfc2898DeriveBytes(encryptedKey, SaltBytes, 1000, HashAlgorithmName.SHA256);
                byte[] key = rfc2898DeriveBytes.CryptDeriveKey("AES", "SHA256", 256, aesAlg.IV);

                // Create AES Crypto Transform to be used in the CryptoStream transform function
                ICryptoTransform cryptoTransform = aesAlg.CreateDecryptor(key, initVector);

                // Create the streams used for encryption.
                int bufferSize     = Math.Min(MaxBufferSize, encryptedDataLength);
                var plainTextBytes = new byte[bufferSize];


                // Protect encryption Key Again
                if (useExternalKey)
                {
                    ProtectedMemory.Protect(encryptedKey, MemoryProtectionScope.SameProcess);
                }

                // Create the streams used for decryption.
                using (var csDecrypt = new CryptoStream(msEncrypted, cryptoTransform, CryptoStreamMode.Read))
                {
                    int decryptedByteCount;
                    while ((decryptedByteCount = csDecrypt.Read(plainTextBytes, 0, plainTextBytes.Length)) > 0)
                    {
                        msDecrypted.Write(plainTextBytes, 0, decryptedByteCount);
                    }
                }

                byte[] validationHash = hashAlg.ComputeHash(msDecrypted);
                dataIsValid = validationHash.AsEnumerable().SequenceEqual(hashBuffer);
                rfc2898DeriveBytes.Dispose();
                msEncrypted.Dispose();
                msMetadata.Dispose();
            }

            return(msDecrypted.ToArray());
        }