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

标签 javascript node.js websocket

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

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

Section 5.2.2 page 35 of the docs 中解释了我必须做的事情。 .

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.

最佳答案

crypto.createHash() function和相关的hash.update()hash.digest()功能:

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

关于javascript - 如何在 node.js 中获取字符串的 sha1 哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6984139/

相关文章:

javascript - 使用 document.location.href 或 window.location 或 window.location.href 在 Chrome 的 beforeunload 期间未重定向到指定的 URL

javascript - Google-Analytics:事件跟踪表单操作

javascript - Websockets 仅返回字符串如何获取字符串形式的数组

node.js - Linux 狂欢 : How to open a websocket connection as client

javascript - 如何使用 AJAX 上传大型 CSV 文件?

javascript - ionic 服务不能正常工作?

node.js - 最新 NodeJS 版本

node.js - 厉害只能处理一个文件吗?

javascript - Nodejs Q Promise catch 从未用 Passport js 调用

python - 如何使用 Python 创建 SSL 套接字服务器