2015-12-07 2 views
1

Использование OpenPGP.js v1.3.0 Я могу успешно создать пару открытого и закрытого ключей и зашифровать/дешифровать в порядке.Вычислить ключ открытого ключа с помощью openpgp.js

Я изо всех сил, чтобы получить идентификатор ключа с помощью этой функции (находится в openpgp.js и public_key.js):

/** 
* Calculates the key id of the key 
* @return {String} A 8 byte key id 
*/ 
PublicKey.prototype.getKeyId = function() { 
    if (this.keyid) { 
    return this.keyid; 
    } 
    this.keyid = new type_keyid(); 
    if (this.version == 4) { 
    this.keyid.read(util.hex2bin(this.getFingerprint()).substr(12, 8)); 
    } else if (this.version == 3) { 
    this.keyid.read(this.mpi[0].write().substr(-8)); 
    } 
    return this.keyid; 
}; 

ОткрытыйКлюч() также в openpgp.js:

/** 
* @constructor 
*/ 
function PublicKey() { 
    this.tag = enums.packet.publicKey; 
    this.version = 4; 
    /** Key creation date. 
    * @type {Date} */ 
    this.created = new Date(); 
    /** A list of multiprecision integers 
    * @type {module:type/mpi} */ 
    this.mpi = []; 
    /** Public key algorithm 
    * @type {module:enums.publicKey} */ 
    this.algorithm = 'rsa_sign'; 
    // time in days (V3 only) 
    this.expirationTimeV3 = 0; 
    /** 
    * Fingerprint in lowercase hex 
    * @type {String} 
    */ 
    this.fingerprint = null; 
    /** 
    * Keyid 
    * @type {module:type/keyid} 
    */ 
    this.keyid = null; 
} 

и пытается получить идентификатор ключа, как это:

var publickey = openpgp.key.readArmored(myPublicKey); 
//var keyID = openpgp.packet.PublicKey(publickey).getKeyId()[0].toHex(); 
var keyID = openpgp.PublicKey(publickey).getKeyId()[0].toHex(); 
console.log(keyID); 

, который дает мне ошибку: TypeError: openpgp.PublicKey не является функцией.

Спасибо.

+0

Как насчет 'publickey.keys [0] .getKeyId(). ToHex();'? –

+0

try: openpgp.PublicKey.publickey.keys [0] .getKeyId(). ToHex() получил errror: openpgp.PublicKey не определено – user2677034

+0

Попробуйте то, что я написал вместо этого ... –

ответ

1

задал вопрос здесь: http://www.mail-archive.com/[email protected]/msg00932.html и получил очень полезный ответ.

var openpgp = window.openpgp; 
var testPublicKey = sessionStorage.getItem('testPublicKey'); 
var foundKeys = openpgp.key.readArmored(testPublicKey).keys; 

if (!foundKeys || foundKeys.length !== 1) { 
    throw new Error("Key not readed, or more than one key"); 
} 

var pubKey = foundKeys[0]; foundKeys = null; 

var getFingerprint = function (key) { 
    // openpgp.key <- Class 
    // key <- Instance received by params 
    return key.primaryKey.fingerprint; 
}; 

var getKeyId = function (key) { 
    return key.primaryKey.getKeyId().toHex(); 
} 
console.log(getFingerprint(pubKey)); 
console.log(getKeyId(pubKey)); 
Смежные вопросы