本文整理汇总了C#中System.Security.Cryptography.HMACMD5类的典型用法代码示例。如果您正苦于以下问题:C# HMACMD5类的具体用法?C# HMACMD5怎么用?C# HMACMD5使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HMACMD5类属于System.Security.Cryptography命名空间,在下文中一共展示了HMACMD5类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetAlgorithmByFunctionName
/// <summary>
/// Every time is created new instance of class to guarantee thread safety
/// </summary>
/// <param name="function"></param>
/// <returns></returns>
private HMAC GetAlgorithmByFunctionName(string function)
{
HMAC a;
switch(Util.Convertion.EnumNameToValue<HMACFunction>(function))
{
case HMACFunction.HMACMD5:
a = new HMACMD5();
break;
case HMACFunction.HMACSHA1:
a = new HMACSHA1();
break;
case HMACFunction.HMACSHA256:
a = new HMACSHA256();
break;
case HMACFunction.HMACSHA384:
a = new HMACSHA384();
break;
case HMACFunction.HMACSHA512:
a = new HMACSHA512();
break;
default:
throw new ArgumentException("Unknown function", "function");
}
return a;
}
示例2: 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));
}
示例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: 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)));
}
示例5: 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();
}
示例6: 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);
}
}
示例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: ComputeHashDigest
public static string ComputeHashDigest(string hashString, string preSharedKey, HashMethod hashMethod)
{
byte[] key = StringToByteArray(preSharedKey);
byte[] data = StringToByteArray(hashString);
byte[] hashDigest;
switch (hashMethod)
{
case HashMethod.HMACMD5:
hashDigest = new HMACMD5(key).ComputeHash(data);
break;
case HashMethod.HMACSHA1:
hashDigest = new HMACSHA1(key).ComputeHash(data);
break;
case HashMethod.MD5:
hashDigest = new MD5CryptoServiceProvider().ComputeHash(data);
break;
case HashMethod.SHA1:
hashDigest = new SHA1CryptoServiceProvider().ComputeHash(data);
break;
default:
throw new InvalidOperationException("Invalid hash method");
}
return (ByteArrayToHexString(hashDigest));
}
示例9: 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;
}
示例10: Compute
public byte[] Compute(int bytes, byte[] secret, string label, byte[][] seeds)
{
/* SEEDSを再構成 */
byte[][] new_seeds = new byte[seeds.Length + 1][];
new_seeds[0] = System.Text.Encoding.ASCII.GetBytes (label);
for (int i = 0; i < seeds.Length; i ++)
new_seeds[i + 1] = seeds[i];
/* secretを分割 */
byte[] S1 = new byte[secret.Length >> 1];
byte[] S2 = new byte[secret.Length >> 1];
Buffer.BlockCopy (secret, 0, S1, 0, S1.Length);
Buffer.BlockCopy (secret, S1.Length, S2, 0, S2.Length);
/* P_SHA1とP_MD5を計算 */
byte[] pSHA1, pMD5;
using (HMACSHA1 hmacSHA1 = new HMACSHA1 (S2, true))
using (HMACMD5 hmacMD5 = new HMACMD5 (S1)) {
pSHA1 = Compute_PHash (bytes, new_seeds, hmacSHA1, 20);
pMD5 = Compute_PHash (bytes, new_seeds, hmacMD5, 16);
}
/* PRFを算出 */
byte[] prf = new byte[bytes];
for (int i = 0; i < prf.Length; i++)
prf[i] = (byte)(pSHA1[i] ^ pMD5[i]);
return prf;
}
示例11: 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);
}
示例12: 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();
}
示例13: HashingAMessageWithASecretKey
public byte[] HashingAMessageWithASecretKey(byte[] iterationNumberByte, byte[] userIdByte)
{
using (var hmac = new HMACMD5(userIdByte))
{
byte[] hash = hmac.ComputeHash(iterationNumberByte);
return hash;
}
}
示例14: CheckB
public void CheckB (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new HMACMD5 ();
algo.Key = key;
byte[] hmac = algo.ComputeHash (data, 0, data.Length);
AssertEquals (testName + "b1", result, hmac);
AssertEquals (testName + "b2", result, algo.Hash);
}
示例15: Initialize
public override void Initialize()
{
base.Initialize();
if (md5 == null)
md5 = new HMACMD5();
else
md5.Initialize();
}