Home > Net >  How to realize the sha encryption algorithm in c #? (according to jsSHA transformation)
How to realize the sha encryption algorithm in c #? (according to jsSHA transformation)

Time:11-22

 



<meta charset="utf-8"/& gt;
JsSHA

<body>
Var key="123456";
Var rawStr="ABC";
The function getHMACStr () {
Var a=new jsSHA (SHA - "1", "TEXT");
Return a.s etHMACKey (key, "TEXT"), a.u pdate (rawStr), al-qeada etHMAC (" B64 ");
}

Document. The write (getHMACStr ());
//the result: 8 a5qsnrnnfqmpnkoy9i6upjbfou=
</script>




Shape:
 
Protected void Page_Load (object sender, EventArgs e)
{
/*
* c # - try to replicate the JavaScript hash - IT tools in c # network
https://www.coder.work/article/2690380

Var key="35353535353535366363636363",
Credentials="web: web",
ShaObj=new jsSHA (credentials, "ASCII"),
Hash=shaObj. GetHMAC (key, "HEX", "SHA - 1" and "HEX");//key and generated hash are hex values
Alert (" Hash: "+ Hash);
*/

Response. Write (" js: 60 c9059c9be9bcd092e00eb7f03492fa3259f459 ");
Response. Write (" & lt; Br/& gt;" );

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.Com puteHash (credentialsBytes);
String hash=BitConverter. ToString (hashmessage) Replace (" - ", string. The Empty). ToLower ();

Response. Write (" c # : "+ hash);
}

The Response. The End ();
}

Public static byte [] StringToByteArray (String hex)
{
Int NumberChars=hex. Length;
Byte [] bytes=new byte [NumberChars/2];
for (int i=0; I & lt; NumberChars; I +=2)
Bytes. [I/2]=the Convert ToByte (hex. Substring (I, 2), 16);
Return bytes;
}


Question: how to c # as the js with atul gawande etHMAC (" B64 ")?

Reference articles:
C # - try to replicate the JavaScript hash in c # - IT tools network
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

CodePudding user response:

Caligatio/jsSHA: A JavaScript/TypeScript implementation of the complete Secure Hash Standard (SHA) family (SHA - 1, SHA - 224/256/384/512, SHA3/384/512-224/256, SHAKE128/256, cSHAKE128/256, and KMAC128/256) with HMAC.

https://github.com/Caligatio/jsSHA

CodePudding user response:

Byte [] hashmessage=hmacsha1.Com puteHash (Encoding UTF8. GetBytes (" ABC "));
Var b64.=the Convert ToBase64String (hashmessage);
The results is consistent with js calculation

CodePudding user response:

refer to the second floor dmankill response:
byte [] hashmessage=hmacsha1.Com puteHash (Encoding. UTF8. GetBytes (" ABC "));
Var b64.=the Convert ToBase64String (hashmessage);
The results consistent with js computing


My result is: 8 l9q2jurevgn6zg15xobcjil1c8=, different?

CodePudding user response:

reference TianYi3G2013 reply: 3/f
Quote: refer to the second floor dmankill response:

Byte [] hashmessage=hmacsha1.Com puteHash (Encoding UTF8. GetBytes (" ABC "));
Var b64.=the Convert ToBase64String (hashmessage);
The results consistent with js computing


My result is: 8 l9q2jurevgn6zg15xobcjil1c8=, different?

  •  Tags:  
  • C#
  • Related