首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用openpgp.js计算公钥的密钥id

使用openpgp.js计算公钥的密钥id
EN

Stack Overflow用户
提问于 2015-12-08 07:35:38
回答 1查看 1.4K关注 0票数 2

使用OpenPGP.js v1.3.0,我可以成功地创建公钥/私钥对并加密/解密ok。

我正在努力使用这个函数(在openpgp.js和public_key.js中找到)来获取密钥id:

代码语言:javascript
复制
/**
 * 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中也有PublicKey():

代码语言:javascript
复制
/**
 * @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;
}

并尝试像这样获取密钥id:

代码语言:javascript
复制
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不是一个函数。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-12-15 01:38:23

我在这里问了一个问题:http://www.mail-archive.com/list@openpgpjs.org/msg00932.html,并收到了一个非常有用的回复。

代码语言:javascript
复制
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 read, or more than one key found");
}

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));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34145389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档