[英]hashing “SHA256” with two parameters
我必須轉換哈希字符串的JAVA函數。
這是一個功能:
private static String hmacSha256(String value, String key) throws NoSuchAlgorithmException, InvalidKeyException {
byte[] keyBytes = key.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(value.getBytes());
return String.format("%0" + (rawHmac.length << 1) + "x", new BigInteger(1, rawHmac));
}
我的疑問是:這個函數有2個參數:
我已經使用過Sha256,但我總是只使用一個參數(一個字符串加密)
請問,我怎樣才能在c#中編寫這個函數,或者有沒有人可以向我解釋邏輯?
謝謝
您可以使用HMACSHA256
類來使其工作:
private static string ComputeHash(string key, string value)
{
var byteKey = Encoding.UTF8.GetBytes(key);
string hashString;
using (var hmac = new HMACSHA256(byteKey))
{
var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(value));
hashString = Convert.ToBase64String(hash);
}
return hashString;
}
這不是簡單的SHA256,這是HMACSHA256,並且已經在.Net中有一個類。 HMACSHA256
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.