簡體   English   中英

使用 openpgp.js 計算公鑰的 key id

[英]Calculate the key id of public key using openpgp.js

使用 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 is not a function。

謝謝。

我在這里問了這個問題: 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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM