프로그래밍/C#
자바스크립트 CryptoJS.HmacSHA256를 C# HMACSHA256 클래스 대응
by zoo10
2021. 10. 28.
자바스크립트 함수
var hash = CryptoJS.HmacSHA256(message, secretKey);
var signature = CryptoJS.enc.Hex.stringify(hash);
C# 대응 함수
private string GetMacSha256()
{
string strKey = "key_문자열";
byte[] bytesKey = Encoding.ASCII.GetBytes(strKey);
string message = "변환할 메시지";
HMACSHA256 hmac = new HMACSHA256(bytesKey);
byte[] data = Encoding.ASCII.GetBytes(message);
MemoryStream stream = new MemoryStream(data);
string signature = hmac.ComputeHash(stream).Aggregate("", (s, e) => s + string.Format("{0:x2}", e), s => s);
return signature;
}