如何使用Node.js的加密库创建scrypt哈希?

TIM*_*MEX 3 javascript hash cryptography node.js

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

我现在正在使用它,但我想将它切换为scrypt.我怎样才能做到这一点?

ytt*_*ium 8

你应该使用node-scrypt.

它有明确的API和良好的文档.

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);
});
Run Code Online (Sandbox Code Playgroud)

  • 来自 `node-scrypt`:“#警告!!!此模块已弃用。相反,请使用 https://nodejs.org/api/crypto.html#crypto_crypto_scrypt_password_salt_keylen_options_callback” (3认同)
  • 警告:截至目前,`scrypt`不支持`node`版本0.11或新发布的版本0.12。 (2认同)