admin管理员组

文章数量:1287561

function sha512(s){
    var sha = crypto.createHash('sha512');
    sha.update(s);
    return sha.digest('hex');
};
exports.sha512 = sha512;

I'm using this right now, but I want to switch it to scrypt. How can I do that?

function sha512(s){
    var sha = crypto.createHash('sha512');
    sha.update(s);
    return sha.digest('hex');
};
exports.sha512 = sha512;

I'm using this right now, but I want to switch it to scrypt. How can I do that?

Share Improve this question asked Feb 15, 2014 at 21:55 TIMEXTIMEX 272k367 gold badges801 silver badges1.1k bronze badges 3
  • Node's crypto library does not support scrypt. And an implementation of scrypt into crypto doesn't seem to be in the future as scrypt isn't standardized yet. You should use node-scrypt which wraps the native C++ scrypt utility. – tsturzl Commented Feb 21, 2014 at 8:38
  • Meanwhile Node's Crypto supports scrypt. Node 10.5+ – smonkey Commented Nov 27, 2018 at 16:36
  • scrypt-kdf is a wrapper around the core Node.js crypto implementation which provides kdf() and verify() functions. – ChrisV Commented Apr 29, 2019 at 10:19
Add a ment  | 

3 Answers 3

Reset to default 8 +350

You should use node-scrypt.

It has clear API and good documentation.

var scrypt = require("scrypt");
var scryptParameters = scrypt.params(0.1);

var key = new Buffer("this is a key"); //key defaults to buffer in config, so input must be a buffer

//Synchronous example that will output in hexidecimal encoding
scrypt.hash.config.outputEncoding = "hex";
var hash = scrypt.hash(key, scryptParameters); //should be wrapped in try catch, but leaving it out for brevity
console.log("Synchronous result: "+hash);

//Asynchronous example that expects key to be ascii encoded
scrypt.hash.config.keyEncoding = "ascii";
scrypt.hash("ascii encoded key", {N: 1, r:1, p:1}, function(err, result){
    //result will be hex encoded
    //Note how scrypt parameters was passed as a JSON object
    console.log("Asynchronous result: "+result);
});

I'll throw my implementation out there: https://www.npmjs/package/scryptsy.

Here's an example:

var scrypt = require('scryptsy') //npm install --save scryptsy

var key = "pleaseletmein" //can be of type 'Buffer'
var salt = "SodiumChloride" //can be of type 'Buffer'
var data = scrypt(key, salt, 16384, 8, 1, 64)
console.log(data.toString('hex')) 
// => 7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887

Documentation here: http://cryptocoinjs./modules/crypto/scryptsy/

Simplified barrysteyn/node-scrypt sync example with (de)serialization:

    let scrypt = require('scrypt')
    let scryptParameters = scrypt.paramsSync(0.1)

    function encode(s) {
      return scrypt.kdfSync(s, scryptParameters).toString('Base64')
    }

    function verify(encoded, s) {
      return scrypt.verifyKdfSync(new Buffer(encoded, 'Base64'), s)
    }

    // Example:
    let s = encode('my password')  // c2NyeXB0AAwAAAAI....
    verify(s, 'my password')   //true
    verify(s, 'my pa$$word')   //false

Tested on node 7.0.0, scrypt 6.0.3.

本文标签: javascriptHow do I make a scrypt hash using Nodejs39s crypto libraryStack Overflow