本文整理汇总了C#中System.Text.UTF8Encoding.ToHmac方法的典型用法代码示例。如果您正苦于以下问题:C# UTF8Encoding.ToHmac方法的具体用法?C# UTF8Encoding.ToHmac怎么用?C# UTF8Encoding.ToHmac使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.UTF8Encoding
的用法示例。
在下文中一共展示了UTF8Encoding.ToHmac方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FromEncryptionAtRest
/// <summary>
/// Decrypt string data with a supported cipher, authenticate the cipher using a supported digest type, deviate the cipher password with a supported divination type, and, finally pad a cipher using a supported padding type.
/// </summary>
/// <param name="data">String data.</param>
/// <param name="password">Cipher password. Will be deviated using the supplied divination type.</param>
/// <param name="salt">Cipher salt to be used, should be proper format for the cipher type. (Note: Normally 8, or, 14 bytes)</param>
/// <param name="secondaryVerifier">Secondary data you wish to use to validate the cipher. This is a secondary validation check. The cipher binary is already validated. (Note: This will increase the size of the cipher)</param>
/// <returns>Byte data</returns>
public static string FromEncryptionAtRest(this string data, string password, byte[] salt, string secondaryVerifier = null)
{
var iv = new UTF8Encoding().GetBytes(data.Substring(0, 16));
var key = password.ToHash(salt);
var suspectedInputVerifier = data.Substring(data.Length - 32, data.Length);
var input = new UTF8Encoding().GetBytes(data.Substring(16, data.Length - 32));
var expectedInputVerifier = input.ToHmac(key.Data, salt);
if (!suspectedInputVerifier.Equals(expectedInputVerifier.Data))
{
return string.Empty;
}
var cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7");
cipher.Init(false, new ParametersWithIV(new KeyParameter(key.Data), iv));
var outputData = cipher.DoFinal(input);
cipher.Reset();
var output = new UTF8Encoding().GetString(outputData);
if (!string.IsNullOrEmpty(secondaryVerifier))
{
var expectedVerifier = secondaryVerifier.ToHmac(key.Data, salt).Data.ToHex();
var suspectedVerifier = output.Substring(output.Length - 64, 64);
if (!expectedVerifier.SequenceEqual(suspectedVerifier))
{
return string.Empty;
}
output = output.Substring(0, output.Length - 64);
}
return output;
}