コード例 #1
0
        /// <summary>
        /// Creates a hash with a specified salt.
        /// </summary>
        /// <param name="plaintext">The plaintext to hash.</param>
        /// <param name="salt">The hash salt.</param>
        /// <returns>The computed hash.</returns>
        protected virtual byte[] CreateHashWithSalt(byte[] plaintext, byte[] salt)
        {
            AddSaltToPlainText(ref salt, ref plaintext);
            HashCryptographer cryptographer = this.HashCryptographer;

            byte[] hash = cryptographer.ComputeHash(plaintext);
            AddSaltToHash(salt, ref hash);
            return(hash);
        }
コード例 #2
0
        public void HashMD5()
        {
            byte[] plaintext = new byte[] {0, 1, 2, 3};
            HashCryptographer cryptographer = new HashCryptographer(typeof(MD5CryptoServiceProvider));
            byte[] hash1 = cryptographer.ComputeHash(plaintext);

            Assert.IsFalse(CryptographyUtility.CompareBytes(plaintext, hash1));

            MD5 md5 = MD5CryptoServiceProvider.Create();
            byte[] hash2 = md5.ComputeHash(plaintext);

            Assert.IsTrue(CryptographyUtility.CompareBytes(hash1, hash2));
        }
コード例 #3
0
        public void HashHMACSHA1()
        {
            byte[] plaintext = new byte[] {0, 1, 2, 3};
            HashCryptographer cryptographer = new HashCryptographer(typeof(HMACSHA1), key);
            byte[] hash1 = cryptographer.ComputeHash(plaintext);

            Assert.IsFalse(CryptographyUtility.CompareBytes(plaintext, hash1));

            KeyedHashAlgorithm hmacsha1 = HMACSHA1.Create();
            hmacsha1.Key = key.DecryptedKey;
            byte[] hash2 = hmacsha1.ComputeHash(plaintext);

            Assert.IsTrue(CryptographyUtility.CompareBytes(hash1, hash2));
        }