diff --git a/src/key.js b/src/key.js index 11a8d1cb..9bcc1e68 100644 --- a/src/key.js +++ b/src/key.js @@ -993,28 +993,31 @@ export function generate(options) { * @return {module:key~Key} * @static */ -export function reformatKey(options) { +export function reformat(options) { var secretKeyPacket, secretSubkeyPacket; - options.keyType = options.keyType || enums.publicKey.rsa_encrypt_sign; - if (options.keyType !== enums.publicKey.rsa_encrypt_sign) { // RSA Encrypt-Only and RSA Sign-Only are deprecated and SHOULD NOT be generated - throw new Error('Only RSA Encrypt or Sign supported'); - } + return Promise.resolve().then(() => { - if (!options.passphrase) { // Key without passphrase is unlocked by definition - options.unlocked = true; - } - if (String.prototype.isPrototypeOf(options.userIds) || typeof options.userIds === 'string') { - options.userIds = [options.userIds]; - } - var packetlist = options.privateKey.toPacketlist(); - for (var i = 0; i < packetlist.length; i++) { - if (packetlist[i].tag === enums.packet.secretKey) { - secretKeyPacket = packetlist[i]; - } else if (packetlist[i].tag === enums.packet.secretSubkey) { - secretSubkeyPacket = packetlist[i]; + options.keyType = options.keyType || enums.publicKey.rsa_encrypt_sign; + if (options.keyType !== enums.publicKey.rsa_encrypt_sign) { // RSA Encrypt-Only and RSA Sign-Only are deprecated and SHOULD NOT be generated + throw new Error('Only RSA Encrypt or Sign supported'); } - } - return wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options); + + if (!options.passphrase) { // Key without passphrase is unlocked by definition + options.unlocked = true; + } + if (String.prototype.isPrototypeOf(options.userIds) || typeof options.userIds === 'string') { + options.userIds = [options.userIds]; + } + var packetlist = options.privateKey.toPacketlist(); + for (var i = 0; i < packetlist.length; i++) { + if (packetlist[i].tag === enums.packet.secretKey) { + secretKeyPacket = packetlist[i]; + } else if (packetlist[i].tag === enums.packet.secretSubkey) { + secretSubkeyPacket = packetlist[i]; + } + } + return wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options); + }); } function wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options) { diff --git a/src/openpgp.js b/src/openpgp.js index 27df2883..aec0f9c4 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -113,6 +113,33 @@ export function generateKey({ userIds=[], passphrase, numBits=2048, unlocked=fal })).catch(onError.bind(null, 'Error generating keypair')); } +/** + * Generates a new OpenPGP key pair. Currently only supports RSA keys. Primary and subkey will be of same type. + * @param {Array} userIds array of user IDs e.g. [{ name:'Phil Zimmermann', email:'phil@openpgp.org' }] + * @param {String} passphrase (optional) The passphrase used to encrypt the resulting private key + * @param {Number} numBits (optional) number of bits for the key creation. (should be 2048 or 4096) + * @param {Boolean} unlocked (optional) If the returned secret part of the generated key is unlocked + * @param {Number} keyExpirationTime (optional) The number of seconds after the key creation time that the key expires + * @return {Promise} The generated key object in the form: + * { key:Key, privateKeyArmored:String, publicKeyArmored:String } + * @static + */ +export function reformatKey({ privateKey, userIds=[], passphrase="", unlocked=false, keyExpirationTime=0 } = {}) { + const options = formatUserIds({ privateKey, userIds, passphrase, unlocked, keyExpirationTime }); + + if (!util.getWebCryptoAll() && asyncProxy) { // use web worker if web crypto apis are not supported + return asyncProxy.delegate('reformatKey', options); + } + + return key.reformat(options).then(newKey => ({ + + key: newKey, + privateKeyArmored: newKey.armor(), + publicKeyArmored: newKey.toPublic().armor() + + })).catch(onError.bind(null, 'Error reformatting keypair')); +} + /** * Unlock a private key with your passphrase. * @param {Key} privateKey the private key that is to be decrypted