Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a scrypt hash using Node.js's crypto library?

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?

like image 909
TIMEX Avatar asked Dec 26 '22 13:12

TIMEX


1 Answers

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);
});
like image 166
Yurij Avatar answered Dec 28 '22 10:12

Yurij