简体   繁体   中英

Node.js and sha1

http://www.php.net/manual/en/function.sha1.php

string sha1 ( string $str [, bool $raw_output = false ] )

If the optional raw_output is set to TRUE, then the sha1 digest is instead returned in raw binary format with a length of 20, otherwise the returned value is a 40-character hexadecimal number.


crypto = require("crypto");
console.log( new Buffer(crypto.createHash('sha1').update("some text").digest()).toString('base64') );
// N8KqY8OHc8KYw5lURzJiw6HCoAV8HmMuw5p3
console.log( new Buffer(crypto.createHash('sha1').update("some text").digest("hex")).toString('base64') );
// MzdhYTYzYzc3Mzk4ZDk1NDQ3MzI2MmUxYTAwNTdjMWU2MzJlZGE3Nw==
console.log( new Buffer(crypto.createHash('sha1').update("some text").digest("base64")).toString('base64') );
// TjZwangzT1kyVlJITW1MaG9BVjhIbU11Mm5jPQ==

<?php
echo base64_encode(sha1("some text"));
// MzdhYTYzYzc3Mzk4ZDk1NDQ3MzI2MmUxYTAwNTdjMWU2MzJlZGE3Nw==
echo base64_encode(sha1("some text", true)); // <-- how to reproduce it on the nodejs?
// N6pjx3OY2VRHMmLhoAV8HmMu2nc=
?>
> crypto.createHash('sha1').update("some text").digest('base64')
'N6pjx3OY2VRHMmLhoAV8HmMu2nc='

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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