【问题标题】:Get string data from generateDataKeyPairWithoutPlaintext()从 generateDataKeyPairWithoutPlaintext() 获取字符串数据
【发布时间】:2021-05-31 08:33:07
【问题描述】:

我正在尝试从 AWS KMS 调用(Node.js SDK)的返回值中获取字符串数据:

const pair = await kms.generateDataKeyPairWithoutPlaintext(params);

它返回 pair.PrivateKeyCiphertextBlobpair.PublicKey 作为 Uint8Array blob。我需要先制作一个 base64 字符串,然后再制作一个纯文本。

我。 想想我得到了第一个:

const buff = Buffer.from(pair.PrivateKeyCiphertextBlob);
const privateKey = buff.toString('base64');

(虽然我不确定)而且我真的很难从第二个中提取纯文本。类似的东西

const publicKey = Buffer.from(pair.PublicKey).toString();

不会产生预期的结果。

我做的第一个对吗?第二个怎么办?

【问题讨论】:

    标签: javascript node.js amazon-web-services key-pair amazon-kms


    【解决方案1】:

    好的,我意识到我真正需要的是 PEM。所以我把这个函数放在一起:

    function generatePem (publicKeyBlob) {
    
       const publicKeyInput= {
           key: publicKeyBlob,
           format: 'der',
           type: 'spki'
       }
    
       const publicKeyObject = Crypto.createPublicKey(publicKeyInput);
    
       const publicKeyExportOptions = {
           format: 'pem',
           type: 'spki'
       }
    
       const pemPublic = publicKeyObject.export(publicKeyExportOptions);
    
       return pemPublic;
    }
    

    基于此gist。我只是将pair.PublicKey 作为参数传递。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 2012-05-16
      • 1970-01-01
      相关资源
      最近更新 更多