本文整理汇总了C#中System.Security.Cryptography.HMACSHA256.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# HMACSHA256.Clear方法的具体用法?C# HMACSHA256.Clear怎么用?C# HMACSHA256.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.HMACSHA256
的用法示例。
在下文中一共展示了HMACSHA256.Clear方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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.
public static void EncodeFile(byte[] key, String sourceFile, String destFile)
{
// Initialize the keyed hash object.
HMACSHA256 myhmacsha256 = new HMACSHA256(key);
FileStream inStream = new FileStream(sourceFile, FileMode.Open);
FileStream outStream = new FileStream(destFile, FileMode.Create);
// Compute the hash of the input file.
byte[] hashValue = myhmacsha256.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);
myhmacsha256.Clear();
// Close the streams
inStream.Close();
outStream.Close();
return;
}
示例2: GenerateHMACSHA256Hash
public static string GenerateHMACSHA256Hash(string Password, string QueueId, string UrlToHash)
{
ValidateInput(QueueId, "QueueId");
ValidateInput(Password, "Password");
ValidateInput(UrlToHash, "UrlToHash");
// Create a random key using a random number generator. This would be the
// secret key shared by sender and receiver.
byte[] secretkey = new Byte[64];
Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(Password, Encoding.UTF8.GetBytes(QueueId));
secretkey = deriveBytes.GetBytes(64);
// Initialize the keyed hash object.
HMACSHA256 myhmacsha256 = new HMACSHA256(secretkey);
byte[] byteStringToHash = Encoding.UTF8.GetBytes(UrlToHash);
byte[] hashValue = myhmacsha256.ComputeHash(byteStringToHash);
myhmacsha256.Clear();
string hashStr = Encoding.ASCII.GetString(hashValue);
string UrlEncodeHashString = HttpUtility.UrlEncode(hashStr);
//not all special characters like = and / are encoded on first UrlEncoding
//UrlEncoding once more fixes the problem......
UrlEncodeHashString = HttpUtility.UrlEncode(UrlEncodeHashString);
return UrlEncodeHashString;
}
示例3: HMACHash
// Hashing a value using HMAC-256
private void HMACHash()
{
ASCIIEncoding encode = new ASCIIEncoding();
HMACSHA256 hmac256 = new HMACSHA256(key);
byte[] uniqueAsByte = encode.GetBytes(unique);
byte[] hashedUnique = hmac256.ComputeHash(uniqueAsByte);
unique = null;
unique = ByteToString(hashedUnique);
hmac256.Clear();
}
示例4: GetAccessTokenBySignedRequest
/// <summary>
/// 站内应用使用SignedRequest获取AccessToken
/// </summary>
/// <param name="signedRequest">SignedRequest</param>
/// <returns></returns>
public AccessToken GetAccessTokenBySignedRequest(string signedRequest) {
var parameters = signedRequest.Split('.');
if (parameters.Length < 2) {
throw new Exception("SignedRequest格式错误。");
}
var encodedSig = parameters[0];
var payload = parameters[1];
var sha256 = new HMACSHA256(Encoding.UTF8.GetBytes(AppSecret));
var expectedSig = Convert.ToBase64String(sha256.ComputeHash(Encoding.UTF8.GetBytes(payload)));
sha256.Clear();
encodedSig = parameters[0].Length%4 == 0
? parameters[0]
: parameters[0].PadRight(parameters[0].Length + (4 - parameters[0].Length%4), '=')
.Replace("-", "+")
.Replace("_", "/");
payload = parameters[1].Length%4 == 0
? parameters[1]
: parameters[1].PadRight(parameters[1].Length + (4 - parameters[1].Length%4), '=')
.Replace("-", "+")
.Replace("_", "/");
if (encodedSig != expectedSig) {
throw new WeiboException("SignedRequest签名验证失败。");
}
var result = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(payload)));
if (result["oauth_token"] == null) {
return null; //throw new WeiboException("没有获取到授权信息,请先进行授权。");
}
var token = new AccessToken();
AccessToken = token.Token = result["oauth_token"].ToString();
token.UID = result["user_id"].ToString();
token.ExpiresIn = Convert.ToInt32(result["expires"].ToString());
return token;
}