Scrypt for Node
#WARNING!!! This module is deprecated. Instead, use https://nodejs.org/api/crypto.html#crypto_crypto_scrypt_password_salt_keylen_options_callback
Scrypt for Node/IO is a native node/io C++ wrapper for Colin Percival's scrypt cryptographic hash utility.
As should be the case with any security tool, this library should be scrutinized by anyone using it. If you find or suspect an issue with the code- please bring it to my attention and I'll spend some time trying to make sure that this tool is as secure as possible.
Version 6 is a major new release. It is by and large compatible with version 5.
Version 6 should work much better on all platforms
Version 5 is a major new release that is not backward compatible with any previous version. Some highlights:
Version 5 is not backward compatible, but it should still be easy to migrate. Please read the api section to see what's changed. One big change that is worth noting is a name change: What used to be called hash has now been changed to kdf and conversely, what was kdf is now called hash.
Scrypt is an advanced crypto library used mainly for key derivation: More information can be found here:
Node-gyp is needed to build this module. It should be installed globally, that is, with the -g switch:
npm install -g node-gypnpm install scryptgit clone https://github.com/barrysteyn/node-scrypt.git
cd node-scrypt
npm install
node-gyp configure buildTo test, go to the folder where scrypt was installed, and type:
npm testTranslates human understandable parameters to scrypt's internal parameters.
scrypt.paramsSync
scrypt.params(maxtime, [maxmem, [max_memfrac]], [function(err, obj) {}])
Note: In previous versions, this was called hash.
Produces a key derivation function that uses the scrypt hash function. This should be used for hashing and checking passwords as it incorporates salt as well as HMAC into its format. It is based on a design by Colin Percival, the author of scrypt. The format can be seen here.
scrypt.kdfSync
scrypt.kdf(key, paramsObject, [function(err, obj){}])
Checks if a key (password) matches a kdf.
scrypt.verifyKdfSync
scrypt.verifyKdf(kdf, key, [function(err, result){}])
Note: In previous versions, this was called kdf.
This is the raw scrypt hash function.
scrypt.hashSync
scrypt.hash(key, paramsObject, output_length, salt, function(err, obj){})
var scrypt = require("scrypt");
//Synchronous
try {
//Uses 0.1 for maxtime, and default values maxmem and maxmemfrac
var scryptParameters = scrypt.paramsSync(0.1);
console.log(scryptParameters);
} catch(err) {
//handle error
}
//Asynchronous with callback
scrypt.params(0.1, function(err, scryptParameters) {
console.log(scryptParameters);
});
//Asynchronous with promise
scrypt.params(0.1).then(function(result){
console.log(result);
}, function(err) {
console.log(err);
});…var scrypt = require("scrypt");
var scryptParameters = scrypt.paramsSync(0.1);
var kdfResult = scrypt.kdfSync("password", scryptParameters);
//Synchronous
scrypt.verifyKdfSync(kdfResult, "password"); // returns true
scrypt.verifyKdfSync(kdfResult, "incorrect password"); // returns false
//Asynchronous
scrypt.verifyKdf(kdfResult, new Buffer("password"), function(err, result) {
//result will be true
});
//Asynchronous with promise
scrypt.verifyKdf(kdfResult, "incorrect password").then(function(result) {
//result will be false
}, function(err) {
});The scrypt paper lists four test vectors to test implementation. This example will show how to produce these test vectors from within this module.
var scrypt = require("scrypt");
var key = new Buffer("");
//Synchronous
var result = scrypt.hashSync(key,{"N":16,"r":1,"p":1},64,"");
console.log(result.toString("hex"));
//Asynchronous
scrypt.hash(key, {"N":16,"r":1,"p":1},64,"", function(err, res) {
console.log(result.toString("hex"));
});
//Asynchronous with promise
scrypt.hash(key, {"N":16,"r":1,"p":1},64,"").then(function(result) {
console.log(result.toString("hex"));
}, function(err){});var scrypt = require("scrypt");
var salt = new Buffer("NaCl");
//Synchronous
var result = scrypt.hashSync("password", {"N":1024,"r":8,"p":16}, 64, salt);
console.log(result.toString("hex"));
scrypt.hash("password", {"N":1024,"r":8,"p":16},64,salt, function(err, result) {
console.log(result.toString("hex"));
});var scrypt = require("scrypt");
var key = new Buffer("pleaseletmein");
var salt = new Buffer("SodiumChloride");
//Synchronous
var result = scrypt.hashSync(key,{"N":16384,"r":8,"p":1},64,salt);
console.log(result.toString("hex"));
//Asynchronous
scrypt.hash(key, {"N":16384,"r":8,"p":1}, 64, salt, function(err, result) {
console.log(result.toString("hex"));
});Note: This test vector is very taxing in terms of resources.
var scrypt = require("scrypt");
//Synchronous
var result = scrypt.hashSync("pleaseletmein",{"N":1048576,"r":8,"p":1},64,"SodiumChloride");
console.log(result.toString("hex"));
//Asynchronous
scrypt.hash("pleaseletmein", {"N":1048576,"r":8,"p":1},64,"SodiumChloride", function(err, result) {
console.log(result.toString("hex"));
});This module supports most posix platforms, as well as Microsoft Windows. It has been tested on the following platforms: Linux, MAC OS, SmartOS (so its ready for Joyent Cloud) and Microsoft Windows. It also works on FreeBSD, OpenBSD, SunOS etc.
It is probably the most advanced key derivation function available. This is is quote taken from a comment in hacker news:
Passwords hashed with scrypt with sufficiently-high strength values (there are 3 tweakable input numbers) are fundamentally impervious to being cracked. I use the word "fundamental" in the literal sense, here; even if you had the resources of a large country, you would not be able to design any hardware (whether it be GPU hardware, custom-designed hardware, or otherwise) which could crack these hashes. Ever. (For sufficiently-small definitions of "ever". At the very least "within your lifetime"; probably far longer.)
No open issues yet, or sync has not completed.