本文整理汇总了C#中System.Security.Cryptography.HMACMD5类的典型用法代码示例。如果您正苦于以下问题:C# HMACMD5类的具体用法?C# HMACMD5怎么用?C# HMACMD5使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HMACMD5类属于System.Security.Cryptography命名空间,在下文中一共展示了HMACMD5类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
}
开发者ID:danni95,项目名称:Core,代码行数:30,代码来源:HMACProvider.cs
示例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));
}
开发者ID:rodolpholl,项目名称:bakerymanager,代码行数:7,代码来源:PasswordHelper.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: 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
示例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();
}
开发者ID:aucnzn,项目名称:1688SDK,代码行数:30,代码来源:Sign.cs
示例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);
}
}
开发者ID:drzo,项目名称:opensim4opencog,代码行数:26,代码来源:CramMD5HexSaslClient.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: 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));
}
开发者ID:dineshkummarc,项目名称:cardsavedotnet,代码行数:27,代码来源:HashUtil.cs
示例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;
}
开发者ID:shi5588,项目名称:shi5588,代码行数:37,代码来源:Program.cs
示例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;
}
开发者ID:kazuki,项目名称:opencrypto-tls,代码行数:28,代码来源:MD5_AND_SHA1.cs
示例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);
}
开发者ID:nexaddo,项目名称:HMACencryption,代码行数:35,代码来源:Authentication.cs
示例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();
}
开发者ID:cembasaranoglu,项目名称:payuloyaltypoint,代码行数:8,代码来源:Helper.cs
示例13: 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
示例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);
}
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:8,代码来源:HMACMD5Test.cs
示例15: Initialize
public override void Initialize()
{
base.Initialize();
if (md5 == null)
md5 = new HMACMD5();
else
md5.Initialize();
}
开发者ID:pengyancai,项目名称:cs-util,代码行数:9,代码来源:CRAMMD5Mechanism.cs
示例16: 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
示例17: NScAPIWrapper
/**
*
* This is the constructor of the class
* Three parameters are needed to build an object of this type
*
* @param key this represents your private. You should contact the NitroSell Support Team to request one.
* @param userid this represents your User ID as assigned by the retailer to which you would like to query his data
* @param webstoreUrl this is the URL of the WebStore that you would like to query
*/
public NScAPIWrapper(string key, string userid, string webstoreUrl)
{
this._key = key;
this._userid = userid;
this._webstoreUrl = webstoreUrl;
var keyByte = encoding.GetBytes(key);
this._hmacmd5 = new HMACMD5(keyByte);
}
开发者ID:NitroSell,项目名称:WebStoreAPI,代码行数:18,代码来源:NScAPIWrapper.cs
示例18: Constructors
public void Constructors ()
{
algo = new HMACMD5 ();
Assert.IsNotNull (algo, "HMACMD5 ()");
byte[] key = new byte [8];
algo = new HMACMD5 (key);
Assert.IsNotNull (algo, "HMACMD5 (key)");
}
开发者ID:nlhepler,项目名称:mono,代码行数:9,代码来源:HMACMD5Test.cs
示例19: 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
示例20: 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类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论