本文整理汇总了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)));
}
示例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);
}
示例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;
}
示例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();
}
示例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;
}
示例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));
}
示例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);
}
示例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);
}
}
示例9: HashingAMessageWithASecretKey
public byte[] HashingAMessageWithASecretKey(byte[] iterationNumberByte, byte[] userIdByte)
{
using (var hmac = new HMACMD5(userIdByte))
{
byte[] hash = hmac.ComputeHash(iterationNumberByte);
return hash;
}
}
示例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();
}
示例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);
}
示例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();
}
}
示例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;
}
示例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;
}
示例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);
}