Javascript 如何在 node.js 中获取字符串的 sha1 哈希值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6984139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:07:34  来源:igfitidea点击:

How can I get the sha1 hash of a string in node.js?

javascriptnode.jswebsocket

提问by Eric

I'm trying to create a websocket server written in node.js

我正在尝试创建一个用 node.js 编写的 websocket 服务器

To get the server to work I need to get the SHA1 hash of a string.

为了让服务器工作,我需要获取字符串的 SHA1 哈希值。

What I have to do is explained in Section 5.2.2 page 35 of the docs.

我必须做的在文档的第 5.2.2 页第 35 节中有解释。

NOTE: As an example, if the value of the "Sec-WebSocket-Key"header in the client's handshake were "dGhlIHNhbXBsZSBub25jZQ==", the server would append thestring "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"to form the string "dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11". The server would then take the SHA-1 hash of this string, giving the value 0xb3 0x7a 0x4f 0x2c 0xc0 0x62 0x4f 0x16 0x90 0xf6 0x46 0x06 0xcf 0x38 0x59 0x45 0xb2 0xbe 0xc4 0xea. This value is then base64-encoded, to give the value "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", which would be returned in the "Sec-WebSocket-Accept"header.

注意:例如,如果"Sec-WebSocket-Key"客户端握手"dGhlIHNhbXBsZSBub25jZQ=="中标头的值为,则服务器将附加字符串"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"以形成字符串"dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11"。然后,服务器将获取该字符串的 SHA-1 哈希值,给出值 0xb3 0x7a 0x4f 0x2c 0xc0 0x62 0x4f 0x16 0x90 0xf6 0x46 0x06 0xcf 0x38 0x59 0x2xe.0x59 0x24 然后将此值进行 base64 编码,以给出值"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",该值将在"Sec-WebSocket-Accept"标头中返回。

回答by maerics

See the crypto.createHash()functionand the associated hash.update()and hash.digest()functions:

请参阅crypto.createHash()功能和相关的hash.update()hash.digest()功能:

var crypto = require('crypto')
var shasum = crypto.createHash('sha1')
shasum.update('foo')
shasum.digest('hex') // => "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"

回答by mikemaccana

Obligatory: SHA1 is broken, you can compute SHA1 collisions for 45,000 USD. You should use sha256:

强制性:SHA1 已损坏,您可以计算 45,000 美元的 SHA1 冲突。你应该使用sha256

var getSHA256ofJSON = function(input){
    return crypto.createHash('sha256').update(JSON.stringify(input)).digest('hex')
}

To answer your question and make a SHA1 hash:

要回答您的问题并进行 SHA1 哈希:

const INSECURE_ALGORITHM = 'sha1'
var getInsecureSHA1ofJSON = function(input){
    return crypto.createHash(INSECURE_ALGORITHM).update(JSON.stringify(input)).digest('hex')
}

Then:

然后:

getSHA256ofJSON('whatever')

or

或者

getSHA256ofJSON(['whatever'])

or

或者

getSHA256ofJSON({'this':'too'})

Official node docs on crypto.createHash()

官方节点文档 crypto.createHash()

回答by Alex Turpin

Please read and strongly consider my advice in the comments of your post. That being said, if you still have a good reason to do this, check out this list of crypto modules for Node. It has modules for dealing with both sha1 and base64.

请阅读并认真考虑我在您帖子的评论中的建议。话虽如此,如果您仍然有充分的理由这样做,请查看Node 的加密模块列表。它有处理 sha1 和 base64 的模块。

回答by A-312

Tips to prevent issue (bad hash) :

防止问题(错误哈希)的提示:

I experienced that NodeJS is hashing the UTF-8 representation of the string. Other languages (like Python, PHP or PERL...) are hashing the byte string.

我经历过 NodeJS 正在对字符串的 UTF-8 表示进行哈希处理。其他语言(如 Python、PHP 或 PERL...)正在对字节字符串进行哈希处理。

We can add binaryargument to use the byte string.

我们可以添加二进制参数来使用字节字符串。

const crypto = require("crypto");

function sha1(data) {
    return crypto.createHash("sha1").update(data, "binary").digest("hex");
}

sha1("Your text ;)");

You can try with : "\xac", "\xd1", "\xb9", "\xe2", "\xbb", "\x93", etc...

您可以尝试使用:“\xac”、“\xd1”、“\xb9”、“\xe2”、“\xbb”、“\x93”等...

Other languages (Python, PHP, ...):

其他语言(Python、PHP...):

sha1("\xac") //39527c59247a39d18ad48b9947ea738396a3bc47

Nodejs:

节点:

sha1 = crypto.createHash("sha1").update("\xac", "binary").digest("hex") //39527c59247a39d18ad48b9947ea738396a3bc47
//without:
sha1 = crypto.createHash("sha1").update("\xac").digest("hex") //f50eb35d94f1d75480496e54f4b4a472a9148752

回答by user944550

You can use:

您可以使用:

  const sha1 = require('sha1');
  const crypt = sha1('Text');
  console.log(crypt);

For install:

安装:

  sudo npm install -g sha1
  npm install sha1 --save