首页 > 解决方案 > Forge生成的证书中的签名和解码

问题描述

我正在使用node-forge生成和验证证书

我正在生成和验证证书,一切似乎都运行良好,但是当涉及到我确实需要为证书提供签名的部分时,它失败了

澄清一下:我需要为证书提供签名,其中签名使用RSA进行散列,并且签名包含有关客户端及其角色的数据

我创建签名的方式

const createSignture = (privateKey, data) => {
  // The signature method takes the data we want to sign, the
  // hashing algorithm, and the padding scheme, and generates
  // a signature in the form of bytes
  const signature = crypto.sign("sha256", Buffer.from(data), {
    key: privateKey,
    padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
  });

  return signature.toString("base64");
};

当我使用 forge 创建证书时,我正在这样做:

const cert = forge.pki.createCertificate();
....
cert.signature = createSignture(privateKey, userUniqueId);
...

当我验证证书时,我正在尝试验证签名

const verifySignture = (publicKey, signture, data) => {
  // To verify the data, we provide the same hashing algorithm and
  // padding scheme we provided to generate the signature, along
  // with the signature itself, the data that we want to
  // verify against the signature, and the public key
  return crypto.verify(
    "sha256",
    Buffer.from(data),
    {
      key: publicKey,
      padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
    },
    signture
  );
};


const certSignture = cert.signature;
const decodedSignature = Buffer.from(forge.util.binary.raw.decode(certSignture)).toString("base64");


verifySignture(publicKey, decodedSignature, userUniqueId) // This always returns false

我不太确定我应该在签名中提供角色、用户 ID 等。

我已经尝试了很多小时,但没有任何效果,所以我非常感谢任何帮助

标签: node.jssslnode-forge

解决方案


推荐阅读