Skip to content

Commit

Permalink
Revert promises to JS, fixes #25 #33 #37
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed Apr 7, 2016
1 parent fbc01bb commit 779cfd7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ module.exports = {

try {
validate(salt, options);
return bindings.hash(plain, salt, options.timeCost, options.memoryCost,
options.parallelism, options.argon2d);
return new Promise((resolve, reject) => {
return bindings.hash(plain, salt, options.timeCost, options.memoryCost,
options.parallelism, options.argon2d, resolve, reject);
});
} catch (err) {
return Promise.reject(err);
}
Expand Down Expand Up @@ -105,7 +107,9 @@ module.exports = {
plain = new Buffer(plain);
}

return bindings.verify(new Buffer(hash), plain, hash[7] === 'd');
return new Promise((resolve, reject) => {
bindings.verify(hash, plain, hash[7] === 'd', resolve, reject);
});
},

verifySync(hash, plain) {
Expand Down
45 changes: 24 additions & 21 deletions src/argon2_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ void HashAsyncWorker::HandleOKCallback()

Nan::HandleScope scope;

auto promise = GetFromPersistent(1).As<Promise::Resolver>();
auto value = Nan::Encode(output.get(), strlen(output.get()));
promise->Resolve(Nan::GetCurrentContext(), value);
Nan::Callback resolve{GetFromPersistent(1).As<Function>()};
Local<Value> argv[] = {Nan::Encode(output.get(), strlen(output.get()))};
resolve.Call(1, argv);
}

/* LCOV_EXCL_START */
Expand All @@ -78,32 +78,33 @@ void HashAsyncWorker::HandleErrorCallback()

Nan::HandleScope scope;

auto promise = GetFromPersistent(1).As<Promise::Resolver>();
auto reason = Nan::New(ErrorMessage()).ToLocalChecked();
promise->Reject(Nan::GetCurrentContext(), Exception::Error(reason));
Nan::Callback reject{GetFromPersistent(2).As<Function>()};
Local<Value> argv[] = {Nan::New(ErrorMessage()).ToLocalChecked()};
reject.Call(1, argv);
}
/* LCOV_EXCL_STOP */

NAN_METHOD(Hash) {
using namespace node;
using namespace v8;

assert(info.Length() >= 6);
assert(info.Length() >= 8);

const auto plain = Nan::To<Object>(info[0]).ToLocalChecked();
const auto salt = Nan::To<Object>(info[1]).ToLocalChecked();
auto resolve = Local<Function>::Cast(info[6]);
auto reject = Local<Function>::Cast(info[7]);

auto worker = new HashAsyncWorker{
{Buffer::Data(plain), Buffer::Length(plain)},
{Buffer::Data(salt), Buffer::Length(salt)},
std::make_tuple(GET_ARG(uint32_t, 2), 1u << GET_ARG(uint32_t, 3),
GET_ARG(uint32_t, 4), GET_ARG(bool, 5) ? Argon2_d : Argon2_i)};

auto resolver = Promise::Resolver::New(Nan::GetCurrentContext()).ToLocalChecked();
worker->SaveToPersistent(1, resolver);
worker->SaveToPersistent(1, resolve);
worker->SaveToPersistent(2, reject);

Nan::AsyncQueueWorker(worker);
info.GetReturnValue().Set(resolver->GetPromise());
}

NAN_METHOD(HashSync) {
Expand Down Expand Up @@ -162,8 +163,9 @@ void VerifyAsyncWorker::HandleOKCallback()

Nan::HandleScope scope;

auto promise = GetFromPersistent(1).As<Promise::Resolver>();
promise->Resolve(Nan::GetCurrentContext(), Nan::New(output));
Nan::Callback resolve{GetFromPersistent(1).As<Function>()};
Local<Value> argv[] = {Nan::New(output)};
resolve.Call(1, argv);
}

/* LCOV_EXCL_START */
Expand All @@ -173,30 +175,31 @@ void VerifyAsyncWorker::HandleErrorCallback()

Nan::HandleScope scope;

auto promise = GetFromPersistent(1).As<Promise::Resolver>();
auto reason = Nan::New(ErrorMessage()).ToLocalChecked();
promise->Reject(Nan::GetCurrentContext(), Exception::Error(reason));
Nan::Callback reject{GetFromPersistent(2).As<Function>()};
Local<Value> argv[] = {Nan::New(ErrorMessage()).ToLocalChecked()};
reject.Call(1, argv);
}
/* LCOV_EXCL_STOP */

NAN_METHOD(Verify) {
using namespace node;
using namespace v8;

assert(info.Length() >= 3);
assert(info.Length() >= 5);

Nan::Utf8String hash{Nan::To<String>(info[0]).ToLocalChecked()};
const auto plain = Nan::To<Object>(info[1]).ToLocalChecked();
auto type = GET_ARG(bool, 2) ? Argon2_d : Argon2_i;
auto resolve = Local<Function>::Cast(info[3]);
auto reject = Local<Function>::Cast(info[4]);

auto worker = new VerifyAsyncWorker(*hash,
{Buffer::Data(plain), Buffer::Length(plain)}, type);
auto worker = new VerifyAsyncWorker{*hash,
{Buffer::Data(plain), Buffer::Length(plain)}, type};

auto resolver = Promise::Resolver::New(Nan::GetCurrentContext()).ToLocalChecked();
worker->SaveToPersistent(1, resolver);
worker->SaveToPersistent(1, resolve);
worker->SaveToPersistent(2, reject);

Nan::AsyncQueueWorker(worker);
info.GetReturnValue().Set(resolver->GetPromise());
}

NAN_METHOD(VerifySync) {
Expand Down
4 changes: 2 additions & 2 deletions test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ t.test('async hash with null in salt', t => {
t.test('async hash with longer salt', t => {
'use strict';

t.plan(1);
t.plan(2);

/* intentionally using a length that is not multiple of 3 */
return argon2.generateSalt(500).then(salt => {
return argon2.hash('password', salt).then(hash => {
t.match(hash, /.*\$.{667}\$/, 'Hash should use the entire salt');
return argon2.verify(hash, 'password').then(t.end);
return argon2.verify(hash, 'password').then(t.ok);
});
});
}).catch(t.threw);
Expand Down

0 comments on commit 779cfd7

Please sign in to comment.