本文整理匯總了C#中System.Security.Cryptography.HashAlgorithm.ToAlgorithmName方法的典型用法代碼示例。如果您正苦於以下問題:C# HashAlgorithm.ToAlgorithmName方法的具體用法?C# HashAlgorithm.ToAlgorithmName怎麽用?C# HashAlgorithm.ToAlgorithmName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Security.Cryptography.HashAlgorithm
的用法示例。
在下文中一共展示了HashAlgorithm.ToAlgorithmName方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: GetCode
internal static int GetCode(
HashAlgorithm algorithm,
string secret,
long counter,
int digits)
{
Contract.Requires<ArgumentOutOfRangeException>(Enum.IsDefined(typeof(HashAlgorithm), algorithm));
Contract.Requires<ArgumentOutOfRangeException>(algorithm != HashAlgorithm.Unknown);
Contract.Requires<ArgumentNullException>(secret != null);
Contract.Requires<ArgumentOutOfRangeException>(counter >= 0);
Contract.Requires<ArgumentOutOfRangeException>(digits > 0);
Contract.Ensures(Contract.Result<int>() > 0);
Contract.Ensures(Contract.Result<int>() < Math.Pow(10, digits));
var generator = HMAC.Create(algorithm.ToAlgorithmName());
generator.Key = Encoding.ASCII.GetBytes(secret);
generator.ComputeHash(CounterToBytes(counter));
var hmac =
generator
.Hash
.Select(b => Convert.ToInt32(b))
.ToArray();
var offset = hmac[19] & 0xF;
var code =
(hmac[offset + 0] & 0x7F) << 24
| (hmac[offset + 1] & 0xFF) << 16
| (hmac[offset + 2] & 0xFF) << 8
| (hmac[offset + 3] & 0xFF);
return code % (int) Math.Pow(10, digits);
}
示例2: CreateSignature
public virtual byte[] CreateSignature(HashAlgorithm hash)
{
if (hash == null)
throw new ArgumentNullException(nameof(hash));
SetHashAlgorithm(hash.ToAlgorithmName());
return CreateSignature(hash.Hash);
}
示例3: VerifySignature
public virtual bool VerifySignature(HashAlgorithm hash, byte[] rgbSignature)
{
if (hash == null)
throw new ArgumentNullException(nameof(hash));
SetHashAlgorithm(hash.ToAlgorithmName());
return VerifySignature(hash.Hash, rgbSignature);
}