Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
feat: generate unique options for a key chain (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider authored and daviddias committed Dec 28, 2017
1 parent de15d12 commit 89a451c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/keychain.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ class Keychain {
return defaultOptions
}

/**
* Generates the options for a keychain. A random salt is produced.
*
* @returns {object}
*/
static generateOptions () {
const options = Object.assign({}, defaultOptions)
const saltLength = Math.ceil(NIST.minSaltLength / 3) * 3 // no base64 padding
options.dek.salt = crypto.randomBytes(saltLength).toString('base64')
return options
}

/**
* Create a new key.
*
Expand Down
7 changes: 7 additions & 0 deletions test/keychain.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ module.exports = (datastore1, datastore2) => {
expect(() => new Keychain(datastore2, { passPhrase: passPhrase, dek: { hash: 'my-hash' } })).to.throw()
})

it('can generate options', () => {
const options = Keychain.generateOptions()
options.passPhrase = passPhrase
const chain = new Keychain(datastore2, options)
expect(chain).to.exist()
})

describe('key name', () => {
it('is a valid filename and non-ASCII', () => {
ks.removeKey('../../nasty', (err) => {
Expand Down

0 comments on commit 89a451c

Please sign in to comment.