add upper level refactorKey function
This commit is contained in:
parent
9de579a29d
commit
a902c421eb
41
src/key.js
41
src/key.js
|
@ -993,28 +993,31 @@ export function generate(options) {
|
||||||
* @return {module:key~Key}
|
* @return {module:key~Key}
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
export function reformatKey(options) {
|
export function reformat(options) {
|
||||||
var secretKeyPacket, secretSubkeyPacket;
|
var secretKeyPacket, secretSubkeyPacket;
|
||||||
options.keyType = options.keyType || enums.publicKey.rsa_encrypt_sign;
|
return Promise.resolve().then(() => {
|
||||||
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');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!options.passphrase) { // Key without passphrase is unlocked by definition
|
options.keyType = options.keyType || enums.publicKey.rsa_encrypt_sign;
|
||||||
options.unlocked = true;
|
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');
|
||||||
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);
|
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) {
|
function wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options) {
|
||||||
|
|
|
@ -113,6 +113,33 @@ export function generateKey({ userIds=[], passphrase, numBits=2048, unlocked=fal
|
||||||
})).catch(onError.bind(null, 'Error generating keypair'));
|
})).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.
|
* Unlock a private key with your passphrase.
|
||||||
* @param {Key} privateKey the private key that is to be decrypted
|
* @param {Key} privateKey the private key that is to be decrypted
|
||||||
|
|
Loading…
Reference in New Issue
Block a user