/// <summary> /// Computes the HMAC MD5 hash value for the specified byte array. /// </summary> /// <param name="bytes">The input to compute the hash code for.</param> /// <returns>The computed hash code as GUID.</returns> /// <remarks> /// One instance of the MD5 Crypto Service Provider /// can't operate properly with multiple simultaneous threads. /// Use lock to solve this problem. /// </remarks> public Guid ComputeHash(byte[] bytes, byte[] hashKeyBytes = null) { byte[] hash; // If HMAC hash key is not supplied then... if (hashKeyBytes == null) { lock (MacProviderLock) // Use default from config file. hash = MacProvider.ComputeHash(bytes); } else { // Create MD5HMAC hash provider. var macProvider = new System.Security.Cryptography.HMACMD5(); macProvider.Key = hashKeyBytes; hash = macProvider.ComputeHash(bytes); } return(new Guid(hash)); }
/// <summary> /// 加密密码 /// </summary> /// <param name="pass">密码</param> /// <param name="passwordFormat">加密方式</param> /// <param name="salt">加密字符串</param> /// <returns></returns> private string EncodePassword(string pass, System.Web.Security.MembershipPasswordFormat passwordFormat, string salt) { if (passwordFormat == System.Web.Security.MembershipPasswordFormat.Clear) { return(pass); } byte[] bIn = System.Text.Encoding.Unicode.GetBytes(pass); byte[] bSalt = Convert.FromBase64String(salt); byte[] bRet = null; if (passwordFormat == System.Web.Security.MembershipPasswordFormat.Hashed) { System.Security.Cryptography.HashAlgorithm hashAlgorithm = this.GetHashAlgorithm(); if (hashAlgorithm is System.Security.Cryptography.KeyedHashAlgorithm) { System.Security.Cryptography.KeyedHashAlgorithm keyedHashAlgorithm = (System.Security.Cryptography.KeyedHashAlgorithm)hashAlgorithm; if (keyedHashAlgorithm.Key.Length == bSalt.Length) { keyedHashAlgorithm.Key = bSalt; } else { if (keyedHashAlgorithm.Key.Length < bSalt.Length) { byte[] bKey = new byte[keyedHashAlgorithm.Key.Length]; Buffer.BlockCopy(bSalt, 0, bKey, 0, bKey.Length); keyedHashAlgorithm.Key = bKey; } else { byte[] bKey = new byte[keyedHashAlgorithm.Key.Length]; int num; for (int i = 0; i < bKey.Length; i += num) { num = Math.Min(bSalt.Length, bKey.Length - i); Buffer.BlockCopy(bSalt, 0, bKey, i, num); } keyedHashAlgorithm.Key = bKey; } } bRet = keyedHashAlgorithm.ComputeHash(bIn); } else { byte[] bAll = new byte[bSalt.Length + bIn.Length]; Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length); Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length); bRet = hashAlgorithm.ComputeHash(bAll); } } else //System.Web.Security.MembershipPasswordFormat.Encrypted { byte[] bAll = new byte[bSalt.Length + bIn.Length]; Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length); Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length); bRet = this.EncryptPassword(bAll); } return(Convert.ToBase64String(bRet)); }
public override byte[] ComputeHash(byte[] buffer) { return(_hmac.ComputeHash(buffer)); }