如何用c#实现这个sha加密算法?(根据jsSHA改造)

TianYi3G2013 2020-05-23 05:29:31

<!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;
}


问题:c#的如何改成像上面js的那样,用 a.getHMAC("B64")的?

参考文章:
c# - 尝试在C#上复制JavaScript哈希 - IT工具网
https://www.coder.work/article/2690380

c# - How do you convert a byte array to a hexadecimal string, and vice versa? - Stack Overflow
https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa/311179#311179
...全文
300 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dmankill 2020-05-29
  • 打赏
  • 举报
回复
引用 3 楼 TianYi3G2013 的回复:
[quote=引用 2 楼 dmankill 的回复:] byte[] hashmessage = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes("abc")); var b64 = Convert.ToBase64String(hashmessage); 得出的结果跟js计算的一致
我的结果是:8L9Q2JUrEVgn6zg15xObcJIL1c8=,不同啊?[/quote]
TianYi3G2013 2020-05-29
  • 打赏
  • 举报
回复
引用 2 楼 dmankill 的回复:
byte[] hashmessage = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes("abc")); var b64 = Convert.ToBase64String(hashmessage); 得出的结果跟js计算的一致
我的结果是:8L9Q2JUrEVgn6zg15xObcJIL1c8=,不同啊?
dmankill 2020-05-29
  • 打赏
  • 举报
回复
byte[] hashmessage = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes("abc")); var b64 = Convert.ToBase64String(hashmessage); 得出的结果跟js计算的一致
TianYi3G2013 2020-05-23
  • 打赏
  • 举报
回复
Caligatio/jsSHA: A JavaScript/TypeScript implementation of the complete Secure Hash Standard (SHA) family (SHA-1, SHA-224/256/384/512, SHA3-224/256/384/512, SHAKE128/256, cSHAKE128/256, and KMAC128/256) with HMAC. https://github.com/Caligatio/jsSHA

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧