【问题标题】:Calculate the key id of public key using openpgp.js使用 openpgp.js 计算公钥的密钥 id
【发布时间】:2016-03-12 18:04:52
【问题描述】:

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

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

/**
 * 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;
};

PublicKey() 也在 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;
}

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

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 不是函数。

谢谢。

【问题讨论】:

  • publickey.keys[0].getKeyId().toHex(); 怎么样?
  • 试过:openpgp.PublicKey.publickey.keys[0].getKeyId().toHex() 得到错误:openpgp.PublicKey 未定义
  • 试试我写的...
  • 给出 TypeError “不是函数”

标签: javascript openpgp.js


【解决方案1】:

我在这里问了这个问题: http://www.mail-archive.com/list@openpgpjs.org/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 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));

【讨论】:

    猜你喜欢
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多