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

Remove couch-pwd dependency #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 29 additions & 19 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
var BPromise = require('bluebird');
var URLSafeBase64 = require('urlsafe-base64');
var uuid = require('uuid');
var pwd = require('couch-pwd');
var crypto = require('crypto');

var keylen = 20;
var size = 16;
var iterations = 10;
var encoding = 'hex';
var digest = 'SHA1';

exports.URLSafeUUID = function() {
return URLSafeBase64.encode(uuid.v4(null, new Buffer(16)));
};
Expand All @@ -16,37 +21,42 @@ exports.hashToken = function(token) {

exports.hashPassword = function (password) {
return new BPromise(function (resolve, reject) {
pwd.hash(password, function (err, salt, hash) {
if (err) {
return reject(err);
}
return resolve({
salt: salt,
derived_key: hash
crypto.randomBytes(size, function(err, salt) {
if (err) return reject(err);

salt = salt.toString('hex');

crypto.pbkdf2(password, salt, iterations, keylen, digest, function(err, hash){
if (err) return reject(err);

return resolve({ salt: salt, derived_key: hash.toString(encoding)});
});
});
});
};

exports.verifyPassword = function (hashObj, password) {
var getHash = BPromise.promisify(pwd.hash, {context: pwd});
var iterations = hashObj.iterations;
var salt = hashObj.salt;
var iterations = hashObj.iterations || 10;

var derived_key = hashObj.derived_key;
if (iterations) {
pwd.iterations(iterations);
}
if(!salt || !derived_key) {
return BPromise.reject(false);
}
return getHash(password, salt)
.then(function (hash) {
if (hash === derived_key) {
return BPromise.resolve(true);

return new BPromise(function (resolve, reject) {
crypto.pbkdf2(password, salt, iterations, keylen, digest, function(err, hash) {
if (err) {
return reject(false);
}

if (hash.toString(encoding) === derived_key) {
return resolve(true);
} else {
return BPromise.reject(false);
return reject(false);
}
});
});
};

exports.getDBURL = function(db) {
Expand Down Expand Up @@ -224,4 +234,4 @@ exports.arrayUnion = function (a, b) {
}
}
return result;
};
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"homepage": "https://github.com/colinskow/superlogin",
"dependencies": {
"bluebird": "^3.3.4",
"couch-pwd": "github:zeMirco/couch-pwd",
"ejs": "^2.3.1",
"express": "^4.13.3",
"extend": "^3.0.0",
Expand Down