Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Promise support, bump to 2.0.1 #40

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Usage

### Modern API

#### scrypt(password, salt, options, callback)
#### scrypt(password, salt, options[, callback])

Derives a key from password and salt and calls callback
with derived key as the only argument.
Expand All @@ -52,12 +52,14 @@ zero setTimeout) at the given interruptSteps to avoid freezing the browser.
If it's not set or set to zero, the callback is called immediately after the
calculation, avoiding setImmediate.

If callback is not set, `scrypt` returns a Promise.

#### Arguments:

* *password* — password (`string` or `Array` of bytes or `Uint8Array`)
* *salt* — salt (`string` or `Array` of bytes or `Uint8Array`)
* *options* — object with key derivation options
* *callback* — callback function receiving result (`function (Array|Uint8Array|string)`)
* *[callback]* — callback function receiving result (`function (Array|Uint8Array|string)`), omit for Promise

##### Options:

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scrypt-async",
"version": "2.0.0",
"version": "2.0.1",
"description": "Fast \"async\" scrypt implementation in JavaScript.",
"main": "scrypt-async.js",
"scripts": {
Expand Down Expand Up @@ -34,5 +34,8 @@
"bugs": {
"url": "https://github.com/dchest/scrypt-async-js/issues"
},
"homepage": "https://github.com/dchest/scrypt-async-js"
"homepage": "https://github.com/dchest/scrypt-async-js",
"dependencies": {
"promise": "^8.0.1"
}
}
23 changes: 18 additions & 5 deletions scrypt-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* Pass 0 to have callback called immediately.
*
*/
var Promise = require('promise');
function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encoding) {
'use strict';

Expand Down Expand Up @@ -398,7 +399,7 @@ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encodin
throw new Error('scrypt: missing N parameter');
}
}
p = opts.p || 1;
p = opts.p;
r = opts.r;
dkLen = opts.dkLen || 32;
interruptStep = opts.interruptStep || 0;
Expand Down Expand Up @@ -542,10 +543,22 @@ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encodin
interruptStep = 1000;
}

if (interruptStep <= 0) {
calculateSync();
} else {
calculateAsync(0);
function run() {
if (interruptStep <= 0) {
calculateSync();
} else {
calculateAsync(0);
}
}

if(!callback){
// Promise
return new Promise(function(resolve) {
callback = resolve;
run();
})
}else {
run();
}
}

Expand Down
2 changes: 1 addition & 1 deletion scrypt-async.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions test/unittests.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,21 @@ function async_test_opts(i, interruptStep, done) {
});
}

function async_test_opts_promise(i, interruptStep, done) {
var v = inputsWithP[i];
scrypt(v.password, v.salt, {
N: v.N,
r: v.r,
p: v.p,
dkLen: v.result.length,
interruptStep: interruptStep,
encoding: v.encoding
}).then(function(out) {
assert.deepEqual(v.result, out);
done();
})
}


describe('async input/output test with options and p with zero interruptStep', function() {
this.timeout(100000);
Expand Down Expand Up @@ -546,3 +561,13 @@ describe('async input/output test with options and p', function() {
// });

});

describe('async promise', function() {
this.timeout(100000);

var step = 0;

it('input 0', function(done) {
async_test_opts_promise(0, step, done);
});
});
Loading