简体   繁体   English

javascript和C#中的SHA1 base64算法

[英]SHA1 base64 algorithm in javascript and C#

I'm running some tests comparing the sha1 algorithm implemented in javascript - got from http://pajhome.org.uk/crypt/ - with its implementation in C#. 我正在运行一些测试,比较javascript中实现的sha1算法 - 来自http://pajhome.org.uk/crypt/ - 在C#中实现。

Using C# to get the hash for {'method':'people.get'} I'm using this statement: 使用C#获取{'method'的哈希值:'people.get'}我正在使用这个语句:

Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes("{'method':'people.get'}")));

which gives me Qy95a0ShZqhbNdt6IF8qNf72jX0= 这给了我Qy95a0ShZqhbNdt6IF8qNf72jX0=

In javascript I get almost the same: Qy95a0ShZqhbNdt6IF8qNf72jX0 using the statement: 在javascript中我得到几乎相同: Qy95a0ShZqhbNdt6IF8qNf72jX0使用语句:

b64_sha1("{'method':'people.get'}");

In the javascript case, the hash doesn't end with a equal (=) sign. 在javascript的情况下,哈希不以等号(=)结束。

Can this difference cause me troubles in authenticating against a server? 这种差异会导致我在对服务器进行身份验证时遇到麻烦吗?

In my case, as many of you may know, the sentence I'm reckoning the hash goes inside the http body and the server will check it. 在我的情况下,正如你们许多人所知,我正在计算哈希的句子进入http体内,服务器将检查它。

Thanks 谢谢

Would depend on the receiver but try decoding your result from the javascript with C# and you'll get an exception. 将取决于接收器,但尝试使用C#从javascript解码您的结果,你会得到一个例外。 The = sign is there to pad the result to the correct length. =符号用于将结果填充到正确的长度。

http://en.wikipedia.org/wiki/Base64 http://en.wikipedia.org/wiki/Base64

The sha1.js from the lib I mentioned in my question, has a global variable called b64pad , used for the purpose of padding. 我在我的问题中提到的lib中的sha1.js有一个名为b64pad的全局变量,用于填充。 By default its value is "" . 默认情况下,其值为"" Changing to "=" gives the exact hash reckoned by C# API 更改为"="会给出C#API计算的确切哈希值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM