110,538
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jsSHA</title>
</head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jsSHA/2.4.2/sha.js"></script>
<script type="text/javascript">
var key = "123456";
var rawStr = "abc";
function getHMACStr() {
var a = new jsSHA("SHA-1", "TEXT");
return a.setHMACKey(key, "TEXT"), a.update(rawStr), a.getHMAC("B64");
}
document.write(getHMACStr());
//结果:8a5qSNRnNFqmPnKoy9i6upJBfOU=
</script>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
/*
* c# - 尝试在C#上复制JavaScript哈希 - IT工具网
https://www.coder.work/article/2690380
var key = "35353535353535366363636363",
credentials = "web:web",
shaObj = new jsSHA(credentials, "ASCII"),
hash = shaObj.getHMAC(key, "HEX", "SHA-1", "HEX"); // key and generated hash are hex values
alert("Hash: " + hash);
*/
Response.Write("js:60c9059c9be9bcd092e00eb7f03492fa3259f459");
Response.Write("<br/>");
var key = "123456";
string credentials = "abc";
var encodingCred = new System.Text.ASCIIEncoding();
var encodingKey = new System.Text.ASCIIEncoding();
//byte[] keyByte = encodingKey.GetBytes(key);
byte[] keyByte = StringToByteArray(key);
byte[] credentialsBytes = encodingCred.GetBytes(credentials);
using (var hmacsha1 = new HMACSHA1(keyByte))
{
byte[] hashmessage = hmacsha1.ComputeHash(credentialsBytes);
string hash = BitConverter.ToString(hashmessage).Replace("-", string.Empty).ToLower();
Response.Write("c#:" + hash);
}
Response.End();
}
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}