本文整理匯總了C#中Algorithm.ToString方法的典型用法代碼示例。如果您正苦於以下問題:C# Algorithm.ToString方法的具體用法?C# Algorithm.ToString怎麽用?C# Algorithm.ToString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Algorithm
的用法示例。
在下文中一共展示了Algorithm.ToString方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Compute
/// <summary>Generates the hash of a text.</summary>
/// <param name="algorithm"><see cref="Algorithm"/> to use.</param>
/// <param name="plaintext">The input to compute the hash code for.</param>
/// <returns>The hash as a hexadecimal string.</returns>
public static string Compute(Algorithm algorithm, byte[] plaintext)
{
if (plaintext == null)
{
throw new ArgumentNullException("plainText", "plainText cannot be null");
}
byte[] hashValue = HashAlgorithm.Create(algorithm.ToString()).ComputeHash(plaintext);
string strRet = string.Empty;
if (hashValue != null)
{
foreach (byte b in hashValue)
{
strRet += String.Format("{0:x2}", b);
}
}
return strRet;
}
示例2: CalculateHMAC
private static string CalculateHMAC(string data, string key, Algorithm algorithm)
{
StringBuilder sb = new StringBuilder();
try
{
HMAC hmac = HMAC.Create(algorithm.ToString());
hmac.Key = key.ToByteArray();
// compute hmac
byte[] rawHmac = hmac.ComputeHash(Encoding.ASCII.GetBytes(data));
// convert to hex string
foreach (var b in rawHmac)
{
sb.AppendFormat("{0:x2}", b);
}
}
catch (Exception ex)
{
throw new Exception("Failed to create token", ex);
}
return sb.ToString();
}
示例3: Create
public static SymmetricAlgorithm Create(Algorithm algorithm)
{
return SymmetricAlgorithm.Create(algorithm.ToString());
}