fix: maps an IPFS hash name to its forge equivalent

Fixes #12
This commit is contained in:
Richard Schneider 2017-12-10 17:19:20 +13:00
parent 3b8d05abb8
commit f71d3a6521
2 changed files with 26 additions and 2 deletions

View File

@ -19,13 +19,26 @@ const NIST = {
minIterationCount: 1000
}
/**
* Maps an IPFS hash name to its forge equivalent.
*
* See https://github.com/multiformats/multihash/blob/master/hashtable.csv
*
* @private
*/
const hashName2Forge = {
'sha1': 'sha1',
'sha2-256': 'sha256',
'sha2-512': 'sha512',
}
const defaultOptions = {
// See https://cryptosense.com/parametesr-choice-for-pbkdf2/
dek: {
keyLength: 512 / 8,
iterationCount: 10000,
salt: 'you should override this value with a crypto secure random number',
hash: 'sha512'
hash: 'sha2-512'
}
}
@ -120,13 +133,18 @@ class Keychain {
}
this.dek = opts.dek
// Get the hashing alogorithm
const hashAlgorithm = hashName2Forge[opts.dek.hash]
if (!hashAlgorithm)
throw new Error(`dek.hash '${opts.dek.hash}' is unknown or not supported`)
// Create the derived encrypting key
let dek = forge.pkcs5.pbkdf2(
opts.passPhrase,
opts.dek.salt,
opts.dek.iterationCount,
opts.dek.keyLength,
opts.dek.hash)
hashAlgorithm)
dek = forge.util.bytesToHex(dek)
Object.defineProperty(this, '_', { value: () => dek })

View File

@ -41,6 +41,12 @@ module.exports = (datastore1, datastore2) => {
expect(Keychain.options).to.exist()
})
it('needs a supported hashing alorithm', () => {
const ok = new Keychain(datastore2, { passPhrase: passPhrase, dek: { hash: 'sha2-256' } })
expect(ok).to.exist()
expect(() => new Keychain(datastore2, { passPhrase: passPhrase, dek: { hash: 'my-hash' } })).to.throw()
})
describe('key name', () => {
it('is a valid filename and non-ASCII', () => {
ks.removeKey('../../nasty', (err) => {