Eri*_*ric 94 javascript websocket node.js
我正在尝试创建一个用node.js编写的websocket服务器
为了使服务器工作,我需要获取字符串的SHA1哈希值.
我需要做的是在文档的第5.2.2节第35页中解释.
注意:例如,如果
"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 0x45 0xb2 0xbe 0xc4 0xea.然后对该值进行base64编码,以提供"s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
将在"Sec-WebSocket-Accept"
标头中返回的值.
mae*_*ics 220
查看crypto.createHash()
功能及相关hash.update()
和hash.digest()
功能:
const crypto = require('crypto')
, shasum = crypto.createHash('sha1');
shasum.update('foo');
console.log(shasum.digest('hex'));
// "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
Run Code Online (Sandbox Code Playgroud)
mik*_*ana 22
必要条件:SHA1已损坏,您可以与普通创业加速器群组的AWS信用额发生冲突,但要回答您的问题:
var getSHA1ofJSON = function(input){
return crypto.createHash('sha1').update(JSON.stringify(input)).digest('hex')
}
Run Code Online (Sandbox Code Playgroud)
然后:
getSHA1ofJSON('whatever')
Run Code Online (Sandbox Code Playgroud)
要么
getSHA1ofJSON(['whatever'])
Run Code Online (Sandbox Code Playgroud)
要么
getSHA1ofJSON({'this':'too'})
Run Code Online (Sandbox Code Playgroud)
A-3*_*312 11
我经历过 NodeJS 正在对字符串的 UTF-8 表示进行哈希处理。其他语言(如 Python、PHP 或 PERL...)正在对字节字符串进行哈希处理。
我们可以添加二进制参数来使用字节字符串。
const crypto = require("crypto");
function sha1(data) {
return crypto.createHash("sha1").update(data, "binary").digest("hex");
}
sha1("Your text ;)");
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用:“\xac”、“\xd1”、“\xb9”、“\xe2”、“\xbb”、“\x93”等...
sha1("\xac") //39527c59247a39d18ad48b9947ea738396a3bc47
Run Code Online (Sandbox Code Playgroud)
sha1 = crypto.createHash("sha1").update("\xac", "binary").digest("hex") //39527c59247a39d18ad48b9947ea738396a3bc47
//without:
sha1 = crypto.createHash("sha1").update("\xac").digest("hex") //f50eb35d94f1d75480496e54f4b4a472a9148752
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用:
const sha1 = require('sha1');
const crypt = sha1('Text');
console.log(crypt);
Run Code Online (Sandbox Code Playgroud)
安装:
sudo npm install -g sha1
npm install sha1 --save
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
84490 次 |
最近记录: |