add openpgp.key.read() function for unarmored serialized keys

This commit is contained in:
Bart Butler 2017-11-06 13:42:07 -08:00
parent 63b387d5b9
commit 58031cfaca

View File

@ -1060,24 +1060,20 @@ SubKey.prototype.update = function(subKey, primaryKey) {
};
/**
* Reads an OpenPGP armored text and returns one or multiple key objects
* @param {String} armoredText text to be parsed
* Reads an unarmored OpenPGP key list and returns one or multiple key objects
* @param {Uint8Array} data to be parsed
* @return {{keys: Array<module:key~Key>, err: (Array<Error>|null)}} result object with key and error arrays
* @static
*/
export function readArmored(armoredText) {
export function read(data) {
var result = {};
result.keys = [];
try {
var input = armor.decode(armoredText);
if (!(input.type === enums.armor.public_key || input.type === enums.armor.private_key)) {
throw new Error('Armored text not of type key');
}
var packetlist = new packet.List();
packetlist.read(input.data);
packetlist.read(data);
var keyIndex = packetlist.indexOfTag(enums.packet.publicKey, enums.packet.secretKey);
if (keyIndex.length === 0) {
throw new Error('No key packet found in armored text');
throw new Error('No key packet found');
}
for (var i = 0; i < keyIndex.length; i++) {
var oneKeyList = packetlist.slice(keyIndex[i], keyIndex[i + 1]);
@ -1096,6 +1092,26 @@ export function readArmored(armoredText) {
return result;
}
/**
* Reads an OpenPGP armored text and returns one or multiple key objects
* @param {String} armoredText text to be parsed
* @return {{keys: Array<module:key~Key>, err: (Array<Error>|null)}} result object with key and error arrays
* @static
*/
export function readArmored(armoredText) {
try {
var input = armor.decode(armoredText);
if (!(input.type === enums.armor.public_key || input.type === enums.armor.private_key)) {
throw new Error('Armored text not of type key');
}
return read(input.data);
} catch (e) {
var result = {keys: [], err: []};
result.err.push(e);
return result;
}
}
/**
* Generates a new OpenPGP key. Currently only supports RSA keys.
* Primary and subkey will be of same type.