Esempio n. 1
0
 /// <summary>
 /// Calculates hash for data contained within an <see cref="EncryptionData"/> with a prefixed
 /// salt value contained within an <see cref="EncryptionData"/>. A "salt" is random data
 /// prefixed to every hashed value to prevent common dictionary attacks.
 /// </summary>
 /// <param name="data">The data to be used to hash.</param>
 /// <param name="salt">The salt to use during the hash.</param>
 /// <returns>a string containing the hash of the data provided.</returns>
 public string Compute(EncryptionData data, EncryptionData salt)
 {
     return(_algorithm.Calculate(data, salt).Hex);
 }
Esempio n. 2
0
 /// <summary>
 /// Calculates hash for fixed length <see cref="EncryptionData"/>.
 /// </summary>
 /// <param name="data">The data to be used to hash.</param>
 /// <returns>the hash of the data provided.</returns>
 public EncryptionData Calculate(EncryptionData data)
 {
     Value.Bytes = _hashAlgorithm.ComputeHash(Check.NotNull(data, nameof(data)).Bytes);
     return(Value);
 }
Esempio n. 3
0
 /// <summary>
 /// Calculates hash for data contained within an <see cref="EncryptionData"/> with a prefixed
 /// salt value contained within an <see cref="EncryptionData"/>. A "salt" is random data
 /// prefixed to every hashed value to prevent common dictionary attacks.
 /// </summary>
 /// <param name="data">The data to be used to hash.</param>
 /// <param name="salt">The salt to use during the hash.</param>
 /// <returns>a byte array containing the hash of the data provided.</returns>
 public byte[] ComputeToBytes(EncryptionData data, EncryptionData salt)
 {
     return(_algorithm.Calculate(data, salt).Bytes);
 }
Esempio n. 4
0
        public EncryptionData Decrypt(EncryptionData publicKey, EncryptionData encrypted, EncryptionData iv)
        {
            if (publicKey == null)
            {
                throw new ArgumentNullException(nameof(publicKey));
            }

            if (encrypted == null)
            {
                throw new ArgumentNullException(nameof(encrypted));
            }

            if (iv == null)
            {
                throw new ArgumentNullException(nameof(iv));
            }

            var decryptedMessage = new EncryptionData
            {
                EncodingToUse = Encoding.UTF8
            };

            var key        = CngKey.Import(publicKey.Bytes, CngKeyBlobFormat.EccPublicBlob);
            var derivedKey = _dh.DeriveKeyMaterial(key);

            _encryptor.Key = derivedKey;
            _encryptor.IV  = iv.Bytes;

            MemoryStream stream = null;

            try
            {
                stream = new MemoryStream();

                using (var decryption = _encryptor.CreateDecryptor())
                {
                    using (var cryptStream = new CryptoStream(stream, decryption, CryptoStreamMode.Write))
                    {
                        cryptStream.Write(encrypted.Bytes, 0, encrypted.Bytes.Length);

                        decryptedMessage.Bytes = stream.ToArray();
                    }
                }
            }
            finally
            {
                stream?.Dispose();
            }

            return(decryptedMessage);
        }