当前位置: 首页>>代码示例>>C#>>正文


C# HMACMD5.ComputeHash方法代码示例

本文整理汇总了C#中System.Security.Cryptography.HMACMD5.ComputeHash方法的典型用法代码示例。如果您正苦于以下问题:C# HMACMD5.ComputeHash方法的具体用法?C# HMACMD5.ComputeHash怎么用?C# HMACMD5.ComputeHash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.HMACMD5的用法示例。


在下文中一共展示了HMACMD5.ComputeHash方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Next

 public override ISaslStep Next(byte[] bytesReceived)
 {
     var kMd5 = new HMACMD5(Encoding.UTF8.GetBytes(_password));
     var computedHash = kMd5.ComputeHash(bytesReceived);
     var passwordHash = BitConverter.ToString(computedHash).ToLower().Replace("-", "");
     return new SecondStep(Encoding.UTF8.GetBytes(string.Concat(_userName, ' ', passwordHash)));
 }
开发者ID:rob-blackbourn,项目名称:JetBlack.Authentication,代码行数:7,代码来源:CramMd5Client.cs

示例2: Encode

        public static string Encode(string publicKey, int choice = 2)
        {
            byte[] hashMessage = null;
            byte[] messageBytes = m_encoding.GetBytes(publicKey);

            switch (choice%6)
            {
                case 0:
                    var hmacmd5 = new HMACMD5(m_keyBytes);
                    hashMessage = hmacmd5.ComputeHash(messageBytes);
                    break;
                case 1:
                    var hmacripedmd160 = new HMACRIPEMD160(m_keyBytes);
                    hashMessage = hmacripedmd160.ComputeHash(messageBytes);
                    break;
                case 2:
                    var hmacsha1 = new HMACSHA1(m_keyBytes);
                    hashMessage = hmacsha1.ComputeHash(messageBytes);
                    break;
                case 3:
                    var hmacsha256 = new HMACSHA256(m_keyBytes);
                    hashMessage = hmacsha256.ComputeHash(messageBytes);
                    break;
                case 4:
                    var hmacsha384 = new HMACSHA384(m_keyBytes);
                    hashMessage = hmacsha384.ComputeHash(messageBytes);
                    break;
                case 5:
                    var hmacsha512 = new HMACSHA512(m_keyBytes);
                    hashMessage = hmacsha512.ComputeHash(messageBytes);
                    break;
            }

            return Convert.ToBase64String(hashMessage);
        }
开发者ID:nexaddo,项目名称:HMACencryption,代码行数:35,代码来源:Authentication.cs

示例3: DecodeFile

 // Decrypt the encoded file and compare to original file.
 /// <summary>
 /// 检查文件是否被篡改
 /// </summary>
 /// <param name="key"></param>
 /// <param name="sourceFile"></param>
 /// <returns>true文件与哈希值一致, false不一致</returns>
 public static bool DecodeFile(byte[] key, String sourceFile)
 {
     // Initialize the keyed hash object.
     HMACMD5 hmacMD5 = new HMACMD5(key);
     // Create an array to hold the keyed hash value read from the file.
     byte[] storedHash = new byte[hmacMD5.HashSize / 8];
     // Create a FileStream for the source file.
     FileStream inStream = new FileStream(sourceFile, FileMode.Open);
     // Read in the storedHash.
     inStream.Read(storedHash, 0, storedHash.Length);
     // Compute the hash of the remaining contents of the file.
     // The stream is properly positioned at the beginning of the content,
     // immediately after the stored hash value.
     byte[] computedHash = hmacMD5.ComputeHash(inStream);
     // compare the computed hash with the stored value
     for (int i = 0; i < storedHash.Length; i++)
     {
         if (computedHash[i] != storedHash[i])
         {
             Console.WriteLine("Hash values differ! Encoded file has been tampered with!");
             return false;
         }
     }
     Console.WriteLine("Hash values agree -- no tampering occurred.");
     return true;
 }
开发者ID:shi5588,项目名称:shi5588,代码行数:33,代码来源:Program.cs

示例4: signMD5

        public string signMD5(Dictionary<string, string> paramDic, string SecurityKey, bool isContent = false)
        {
            byte[] signatureKey = Encoding.UTF8.GetBytes(SecurityKey);//此处用自己的签名密钥
            List<string> list = new List<string>();
            foreach (KeyValuePair<string, string> kv in paramDic)
            {
                if (kv.Key.ToLower() != "sign")
                    list.Add(kv.Key + kv.Value);
            }
            list.Sort();
            StringBuilder tmp = new StringBuilder();
            foreach (string kvstr in list)
            {
                tmp.Append(kvstr);
            }

            //HMAC-MD5
            HMACMD5 hmacmd5 = new HMACMD5(signatureKey);
            hmacmd5.ComputeHash(Encoding.UTF8.GetBytes(tmp.ToString()));
            /*
            hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(urlPath));
            foreach (string kvstr in list)
            {
                hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(kvstr));
            }
             */
            byte[] hash = hmacmd5.Hash;
            //TO HEX
            return BitConverter.ToString(hash).Replace("-", string.Empty).ToUpper();
        }
开发者ID:aucnzn,项目名称:1688SDK,代码行数:30,代码来源:Sign.cs

示例5: EncodeFile

 // Computes a keyed hash for a source file, creates a target file with the keyed hash
 // prepended to the contents of the source file, then decrypts the file and compares
 // the source and the decrypted files.
 /// <summary>
 ///  文件加密
 /// </summary>
 /// <param name="key"></param>
 /// <param name="sourceFile">待加密的文件</param>
 /// <param name="destFile">加密后的文件</param>
 public static void EncodeFile(byte[] key, String sourceFile, String destFile)
 {
     // Initialize the keyed hash object.
     HMACMD5 myhmacMD5 = new HMACMD5(key);
     FileStream inStream = new FileStream(sourceFile, FileMode.Open);
     FileStream outStream = new FileStream(destFile, FileMode.Create);
     // Compute the hash of the input file.
     byte[] hashValue = myhmacMD5.ComputeHash(inStream);
     // Reset inStream to the beginning of the file.
     inStream.Position = 0;
     // Write the computed hash value to the output file.
     outStream.Write(hashValue, 0, hashValue.Length);
     // Copy the contents of the sourceFile to the destFile.
     int bytesRead;
     // read 1K at a time
     byte[] buffer = new byte[1024];
     do
     {
         // Read from the wrapping CryptoStream.
         bytesRead = inStream.Read(buffer, 0, 1024);
         outStream.Write(buffer, 0, bytesRead);
     } while (bytesRead > 0);
     myhmacMD5.Clear();
     // Close the streams
     inStream.Close();
     outStream.Close();
     return;
 }
开发者ID:shi5588,项目名称:shi5588,代码行数:37,代码来源:Program.cs

示例6: CryptoMD5

        public static string CryptoMD5(string TextToCryptograph)
        {
            var md5 = new HMACMD5();
            byte[] passwordArray = System.Text.Encoding.Default.GetBytes(TextToCryptograph);

            return Convert.ToBase64String(md5.ComputeHash(passwordArray));
        }
开发者ID:rodolpholl,项目名称:bakerymanager,代码行数:7,代码来源:PasswordHelper.cs

示例7: ValidateResponse

        public bool ValidateResponse(string password)
        {
            HMACMD5 hmacmd5 = new HMACMD5(ASCIIEncoding.ASCII.GetBytes(password));
            string expectedResponse = BitConverter.ToString(hmacmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(Challenge))).Replace("-", "");

            return string.Equals(expectedResponse, ChallengeResponse, StringComparison.InvariantCultureIgnoreCase);
        }
开发者ID:enbiso,项目名称:dummy-smtp-server,代码行数:7,代码来源:CramMd5AuthenticationCredentials.cs

示例8: EvaluateChallenge

      public override byte[] EvaluateChallenge(byte[] challenge)
      {
        if ( challenge == null || challenge.Length == 0 )
            throw new ArgumentNullException("challenge");


        NameCallback nameCB = new NameCallback(AuthorizationId);
        PasswordCallback pwdCB = new PasswordCallback();
        ISaslCallback[] callbacks = { nameCB, pwdCB };
        Handler.Handle(callbacks);

        string username = nameCB.Text;
        
        //Encode the Hashed Password as Hex
        byte[] passwd = Encoding.UTF8.GetBytes(ToHex(pwdCB.HashedText));

        string s = System.Text.UTF8Encoding.UTF8.GetString(challenge);
        
        using ( HMAC hmac = new HMACMD5(passwd) )
         {
            byte[] value = hmac.ComputeHash(challenge);
            string encoded = ToHex(value);
            SetComplete();
            return Encoding.UTF8.GetBytes(username + " " + encoded);
         }
      }
开发者ID:drzo,项目名称:opensim4opencog,代码行数:26,代码来源:CramMD5HexSaslClient.cs

示例9: HashingAMessageWithASecretKey

 public byte[] HashingAMessageWithASecretKey(byte[] iterationNumberByte, byte[] userIdByte)
 {
     using (var hmac = new HMACMD5(userIdByte))
     {
         byte[] hash = hmac.ComputeHash(iterationNumberByte);
         return hash;
     }
 }
开发者ID:jobairkhan,项目名称:TimeBasedOneTimePassword,代码行数:8,代码来源:HashMD5.cs

示例10: Hash

 public static string Hash(string key, string message)
 {
     UTF8Encoding encoding = new UTF8Encoding();
     var messageBytes = encoding.GetBytes(message);
     var md5Hasher = new HMACMD5(encoding.GetBytes(key));
     var hashBytes = md5Hasher.ComputeHash(messageBytes);
     return new string(hashBytes.SelectMany(b => b.ToString("X2")).ToArray()).ToLower();
 }
开发者ID:cembasaranoglu,项目名称:payuloyaltypoint,代码行数:8,代码来源:Helper.cs

示例11: CheckA

		public void CheckA (string testName, byte[] key, byte[] data, byte[] result) 
		{
			algo = new HMACMD5 ();
			algo.Key = key;
			byte[] hmac = algo.ComputeHash (data);
			AssertEquals (testName + "a1", result, hmac);
			AssertEquals (testName + "a2", result, algo.Hash);
		}
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:8,代码来源:HMACMD5Test.cs

示例12: HMACMD5

        /// <summary>
        /// Creates an HMAC-MD5 fingerprint of the given data with the given key using the specified encoding
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key"></param>
        /// <param name="enc"></param>
        /// <returns></returns>
        public static string HMACMD5(this string data, string key, Encoding enc)
        {
            var hmacKey = enc.GetBytes(key);
            var hmacData = enc.GetBytes(data);

            using (var hmacMd5 = new HMACMD5(hmacKey)) {
                return hmacMd5.ComputeHash(hmacData).ToHex().ToLower();
            }
        }
开发者ID:veracross,项目名称:ncontrib,代码行数:16,代码来源:StringCryptographyExtensions.cs

示例13: DecodeFile

        /// <summary>
        /// Decrypt the encoded file and compare to original file. It returns false if the file is corrupted.
        /// </summary>
        /// <param name="key">The key used to encode the file</param>
        /// <param name="sourceFile">The file to decrypt complete path</param>
        /// <param name="destFile">Destination file complete path. If the file doesn't exist, it creates it</param>
        /// <returns></returns>
        public bool DecodeFile(string key, String sourceFile, String destFile)
        {
            if (sourceFile.IsNullOrWhiteSpace() || !File.Exists(sourceFile))
                throw new FileNotFoundException("Cannot find the specified source file", sourceFile ?? "null");

            if (destFile.IsNullOrWhiteSpace())
                throw new ArgumentException("Please specify the path of the output path", nameof(destFile));

            if (string.IsNullOrEmpty(key))
                throw new ArgumentException("Please specify the key", nameof(key));

            // Create a key using a random number generator. This would be the
            //  secret key shared by sender and receiver.
            byte[] secretkey = key.ToByteArray();

            // Initialize the keyed hash object.
            HMACMD5 hmacMD5 = new HMACMD5(secretkey);
            // Create an array to hold the keyed hash value read from the file.
            byte[] storedHash = new byte[hmacMD5.HashSize / 8];
            // Create a FileStream for the source file.
            FileStream inStream = new FileStream(sourceFile, FileMode.Open);
            // Read in the storedHash.
            inStream.Read(storedHash, 0, storedHash.Length);
            // Compute the hash of the remaining contents of the file.
            // The stream is properly positioned at the beginning of the content,
            // immediately after the stored hash value.
            byte[] computedHash = hmacMD5.ComputeHash(inStream);
            // compare the computed hash with the stored value
            int i;
            for (i = 0; i < storedHash.Length; i++)
            {
                if (computedHash[i] != storedHash[i])
                {
                    inStream.Close();
                    return false;
                }
            }

            FileStream outStream = new FileStream(destFile, FileMode.Create);
            // Reset inStream to the beginning of the file.
            inStream.Position = i;
            // Copy the contents of the sourceFile to the destFile.
            int bytesRead;
            // read 1K at a time
            byte[] buffer = new byte[1024];
            do
            {
                // Read from the wrapping CryptoStream.
                bytesRead = inStream.Read(buffer, 0, 1024);
                outStream.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);
            // Close the streams
            inStream.Close();
            outStream.Close();
            return true;
        }
开发者ID:n3wt0n,项目名称:Crypto,代码行数:63,代码来源:HMACMD5.cs

示例14: authenticate

 /// <summary>
 /// Authenticate packet and return authentication parameters value to the caller
 /// </summary>
 /// <param name="authenticationSecret">User authentication secret</param>
 /// <param name="engineId">SNMP agent authoritative engine id</param>
 /// <param name="wholeMessage">Message to authenticate</param>
 /// <returns>Authentication parameters value</returns>
 public byte[] authenticate(byte[] authenticationSecret, byte[] engineId, byte[] wholeMessage)
 {
     byte[] result = new byte[12];
     byte[] authKey = PasswordToKey(authenticationSecret, engineId);
     HMACMD5 md5 = new HMACMD5(authKey);
     byte[] hash = md5.ComputeHash(wholeMessage);
     // copy 12 bytes of the hash into the wholeMessage
     Buffer.BlockCopy(hash, 0, result, 0, 12);
     return result;
 }
开发者ID:griffina,项目名称:SnmpSharpNet,代码行数:17,代码来源:AuthenticationMD5.cs

示例15: DoEncrypt

        /// <summary>
        /// 
        /// </summary>
        /// <param name="plainText"></param>
        /// <param name="key"></param>
        /// <param name="encoding"></param>
        /// <param name="encryptedType"></param>
        /// <returns></returns>
        public override string DoEncrypt(string plainText, string key, Encoding encoding, DataMode encryptedType)
        {
            byte[] keyByte = encoding.GetBytes(key);
            HMACMD5 hmacMD5 = new HMACMD5(keyByte);

            byte[] messageBytes = encoding.GetBytes(plainText);
            byte[] hashMessage = hmacMD5.ComputeHash(messageBytes);

            return BytesToString(hashMessage, encoding, encryptedType);
        }
开发者ID:weiliji,项目名称:MyFramework,代码行数:18,代码来源:HMACMD5HashCryptographer.cs


注:本文中的System.Security.Cryptography.HMACMD5.ComputeHash方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。