From 6d78f54ffcd5214a373207aa6eb99a5ff260f368 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Thu, 7 Dec 2017 11:29:14 +1300 Subject: [PATCH] refactor: its a keychain --- README.md | 3 +-- src/cms.js | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index de4b333..569311c 100644 --- a/README.md +++ b/README.md @@ -88,8 +88,7 @@ A private key is stored as an encrypted PKCS 8 structure in the PEM format. It i The default options for generating the derived encryption key are in the `dek` object ```js const defaultOptions = { - createIfNeeded: true, - + //See https://cryptosense.com/parameter-choice-for-pbkdf2/ dek: { keyLength: 512 / 8, diff --git a/src/cms.js b/src/cms.js index fb66c2d..3a08104 100644 --- a/src/cms.js +++ b/src/cms.js @@ -5,12 +5,12 @@ const forge = require('node-forge') const util = require('./util') class CMS { - constructor (keystore) { - if (!keystore) { - throw new Error('keystore is required') + constructor (keychain) { + if (!keychain) { + throw new Error('keychain is required') } - this.keystore = keystore + this.keychain = keychain } createAnonymousEncryptedData (name, plain, callback) { @@ -19,13 +19,13 @@ class CMS { return callback(new Error('Data is required')) } - self.keystore._getPrivateKey(name, (err, key) => { + self.keychain._getPrivateKey(name, (err, key) => { if (err) { return callback(err) } try { - const privateKey = forge.pki.decryptRsaPrivateKey(key, self.keystore._()) + const privateKey = forge.pki.decryptRsaPrivateKey(key, self.keychain._()) util.certificateForKey(privateKey, (err, certificate) => { if (err) return callback(err) @@ -73,18 +73,18 @@ class CMS { }) async.detect( recipients, - (r, cb) => self.keystore.findKeyById(r.keyId, (err, info) => cb(null, !err && info)), + (r, cb) => self.keychain.findKeyById(r.keyId, (err, info) => cb(null, !err && info)), (err, r) => { if (err) return callback(err) if (!r) return callback(new Error('No key found for decryption')) async.waterfall([ - (cb) => self.keystore.findKeyById(r.keyId, cb), - (key, cb) => self.keystore._getPrivateKey(key.name, cb) + (cb) => self.keychain.findKeyById(r.keyId, cb), + (key, cb) => self.keychain._getPrivateKey(key.name, cb) ], (err, pem) => { if (err) return callback(err) - const privateKey = forge.pki.decryptRsaPrivateKey(pem, self.keystore._()) + const privateKey = forge.pki.decryptRsaPrivateKey(pem, self.keychain._()) cms.decrypt(r.recipient, privateKey) async.setImmediate(() => callback(null, Buffer.from(cms.content.getBytes(), 'binary'))) })