add upper level refactorKey function

This commit is contained in:
Sanjana Rajan 2017-01-31 14:14:27 -08:00
parent 9de579a29d
commit a902c421eb
2 changed files with 49 additions and 19 deletions

View File

@ -993,8 +993,10 @@ export function generate(options) {
* @return {module:key~Key}
* @static
*/
export function reformatKey(options) {
export function reformat(options) {
var secretKeyPacket, secretSubkeyPacket;
return Promise.resolve().then(() => {
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');
@ -1015,6 +1017,7 @@ export function reformatKey(options) {
}
}
return wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options);
});
}
function wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options) {

View File

@ -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<Object>} 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<Object>} 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