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

Commit 89a451c

Browse files
richardschneiderdaviddias
authored andcommitted
feat: generate unique options for a key chain (#20)
1 parent de15d12 commit 89a451c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/keychain.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ class Keychain {
138138
return defaultOptions
139139
}
140140

141+
/**
142+
* Generates the options for a keychain. A random salt is produced.
143+
*
144+
* @returns {object}
145+
*/
146+
static generateOptions () {
147+
const options = Object.assign({}, defaultOptions)
148+
const saltLength = Math.ceil(NIST.minSaltLength / 3) * 3 // no base64 padding
149+
options.dek.salt = crypto.randomBytes(saltLength).toString('base64')
150+
return options
151+
}
152+
141153
/**
142154
* Create a new key.
143155
*

test/keychain.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ module.exports = (datastore1, datastore2) => {
4646
expect(() => new Keychain(datastore2, { passPhrase: passPhrase, dek: { hash: 'my-hash' } })).to.throw()
4747
})
4848

49+
it('can generate options', () => {
50+
const options = Keychain.generateOptions()
51+
options.passPhrase = passPhrase
52+
const chain = new Keychain(datastore2, options)
53+
expect(chain).to.exist()
54+
})
55+
4956
describe('key name', () => {
5057
it('is a valid filename and non-ASCII', () => {
5158
ks.removeKey('../../nasty', (err) => {

0 commit comments

Comments
 (0)