-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from j3k0/feature/bcrypt
`bcrypt` support
- Loading branch information
Showing
8 changed files
with
232 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
const hashes = { | ||
bcrypt: 'detect-hash::hash-bcrypt', | ||
pbkdf2: 'detect-hash::hash-pbkdf2', | ||
plainText: 'detect-hash::plain-text' | ||
}; | ||
|
||
const regexes = { | ||
// https://en.wikipedia.org/wiki/Bcrypt | ||
// https://www.npmjs.com/package/bcrypt#hash-info | ||
// I added `=` since it is base64 (though it probably won't have padding) | ||
bcrypt: /^\$2[aby]\$\d\d\$[A-Za-z0-9./=]{53}$/, | ||
// https://www.npmjs.com/package/password-hash-and-salt#created-hash | ||
// Looks like hex to me. | ||
pbkdf2: /^pbkdf2\$10000\$[0-9a-f]{128}\$[0-9a-f]{128}$/, | ||
}; | ||
|
||
const detectHash = (str) => { | ||
if (typeof str !== 'string') | ||
throw new TypeError(`\`str\` must be a string, got \`${typeof str}\` instead`); | ||
|
||
const kind = Object.keys(regexes).find(hashKind => regexes[hashKind].test(str)); | ||
|
||
return kind | ||
? hashes[kind] | ||
: hashes.plainText; | ||
}; | ||
|
||
module.exports = {detectHash, hashes}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
const bcrypt = require('bcrypt'); | ||
const pbkdf = require('password-hash-and-salt'); | ||
const {detectHash, hashes} = require('./detect-hash'); | ||
|
||
const verifiers = { | ||
[hashes.bcrypt]: (plain, hash, cb) => bcrypt.compare(plain, hash, cb), | ||
[hashes.pbkdf2]: (plain, hash, cb) => pbkdf(plain).verifyAgainst(hash, cb) | ||
}; | ||
|
||
const wrapVerifierCallback = (cb) => (err, matches) => { | ||
return err | ||
? cb(err) | ||
: cb(null, !!matches); | ||
}; | ||
|
||
// callback(err, matches: Boolean) | ||
module.exports = (plainText, hash, cb) => { | ||
const verifier = verifiers[detectHash(hash)]; | ||
const callback = wrapVerifierCallback(cb); | ||
|
||
if (!verifier) | ||
return setImmediate(callback, new Error('Uknown Hash Format')); | ||
|
||
verifier(plainText, hash, callback); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const {detectHash, hashes} = require('../../src/users/detect-hash'); | ||
|
||
describe('detectHash()', () => { | ||
it('exports `hash` that contains valid stuff', () => { | ||
const keys = Object.keys(hashes); | ||
expect(hashes).to.be.an('object'); | ||
expect(hashes).to.be.ok; | ||
expect(keys.length).to.be.greaterThan(0); | ||
keys.forEach(key => { | ||
const val = hashes[key]; | ||
expect(val).to.be.a('string'); | ||
expect(val.length).to.be.greaterThan(0); | ||
}); | ||
}); | ||
|
||
it('detects pbkdf2', () => { | ||
[ | ||
'pbkdf2$10000$5786bea278988484b2ef02fffa2cbc759e4f20bfe3464f43e3b9e56356a09957ce2d77e78c743d7caad381a8818994d55c72e6543ab8a133958494436e997b79$8e8234c0675ec1af532f83acf9db08dec2e07ed425acbba3a53dff1f78921ccc021169312fc144109de1890c8f58b17745a8df250ab7f86cc13515e550dd79aa', | ||
'pbkdf2$10000$1ac47a036e2db28617a9973b87e5cb8b64bca4f9d2d07896773c29ac162a070a3f537f2074c29d39cda3891fe74d3153d834e2e410d9be9c13ef01c650ab38a8$73ba4660abc46cd0550df78e494991c1b3f9ff09b54afb66b9ebfe7528fc30a97437699b1531119e72964cb0a44864f4223be628b16e6e76578d982b30275c74' | ||
].forEach(hash => expect(detectHash(hash)).to.equal(hashes.pbkdf2)); | ||
}); | ||
|
||
it('detects bcrypt', () => { | ||
[ | ||
'$2a$12$HHKaaYswPqVJFROlS/4ZbuN5I2XWb3NE9ChTZ5m174ZRVDSqbGd16', | ||
'$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy', | ||
'$2a$04$tjJzUHkOoR.xzyMTCDxfSu6Vq4NL1u3/9m2NT9bu7.ECODNEWi41K' | ||
].forEach(hash => expect(detectHash(hash)).to.equal(hashes.bcrypt)); | ||
}); | ||
|
||
it('detects plainText', () => { | ||
[ | ||
'pbkdf2$10000$_NOT_QUITE_5786bea278988484b2ef02fffa2cbc759e4f20bfe3464f43e3b9e56356a09957ce2d77e78c743d7caad381a8818994d55c72e6543ab8a133958494436e997b79$8e8234c0675ec1af532f83acf9db08dec2e07ed425acbba3a53dff1f78921ccc021169312fc144109de1890c8f58b17745a8df250ab7f86cc13515e550dd79aa', | ||
'$2a$10$_SOMETHING_FISHY_N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy', | ||
'', | ||
'plain text' | ||
].forEach(hash => expect(detectHash(hash)).to.equal(hashes.plainText)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
const verifyPassword = require('../../src/users/verify-password'); | ||
|
||
describe('verifyPassword()', () => { | ||
const passwords = { | ||
correct: 'pwd', | ||
wrong: 'banana' | ||
}; | ||
|
||
const hashes = { | ||
pbkdf2: 'pbkdf2$10000$5786bea278988484b2ef02fffa2cbc759e4f20bfe3464f43e3b9e56356a09957ce2d77e78c743d7caad381a8818994d55c72e6543ab8a133958494436e997b79$8e8234c0675ec1af532f83acf9db08dec2e07ed425acbba3a53dff1f78921ccc021169312fc144109de1890c8f58b17745a8df250ab7f86cc13515e550dd79aa', | ||
bcrypt: '$2a$04$tjJzUHkOoR.xzyMTCDxfSu6Vq4NL1u3/9m2NT9bu7.ECODNEWi41K', | ||
malformed: 'he he i am bad hash; in fact i am no hash at all!' | ||
}; | ||
|
||
it('pbkdf2 correct password', (done) => { | ||
verifyPassword(passwords.correct, hashes.pbkdf2, (err, matches) => { | ||
expect(err).to.be.null; | ||
expect(matches).to.be.true; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('pbkdf2 wrong password', (done) => { | ||
verifyPassword(passwords.wrong, hashes.pbkdf2, (err, matches) => { | ||
expect(err).to.be.null; | ||
expect(matches).to.be.false; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('bcrypt correct password', (done) => { | ||
verifyPassword(passwords.correct, hashes.bcrypt, (err, matches) => { | ||
expect(err).to.be.null; | ||
expect(matches).to.be.true; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('bcrypt wrong password', (done) => { | ||
verifyPassword(passwords.wrong, hashes.bcrypt, (err, matches) => { | ||
expect(err).to.be.null; | ||
expect(matches).to.be.false; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('errors on wierd looking hashes', (done) => { | ||
verifyPassword(passwords.correct, hashes.malformed, (err, matches) => { | ||
expect(err).to.be.instanceof(Error); | ||
expect(err).to.have.property('message', 'Uknown Hash Format'); | ||
expect(matches).to.be.undefined; | ||
done(); | ||
}); | ||
}); | ||
}); |