How to use Forge library and pfx file to sign plain text?

16 Views Asked by At
    const pkcs12 = forge.pkcs12;
    const pkcs12Der = arrayBufferToString(pfxData)
    const p12Asn1 = forge.asn1.fromDer(pkcs12Der);
    const p12 = pkcs12.pkcs12FromAsn1(p12Asn1, password);
    const certBags = p12.getBags({ bagType: forge.pki.oids.certBag });
    const cert = certBags\[forge.pki.oids.certBag\]\[0\].cert;
    const keyBags = p12.getBags({ bagType: forge.pki.oids.pkcs8ShroudedKeyBag });
    const key = keyBags\[forge.pki.oids.pkcs8ShroudedKeyBag\]\[0\].key;

    const signer = forge.pki.createSigner({
       md: forge.md.sha256.create(),
       rsaOptions: {
       key: forge.pki.privateKeyToPem(key),
     },
    });
    
    signer.certificates = \[cert\];
    
    signer.update(text);
    const signature = signer.sign();
    
    console.log(signature);

enter image description here

'pfxData' is the binary data of the pfx file. 'password' is password of the pfx file. 'text' is the text to be signed. Which method should be used for signing?

0

There are 0 best solutions below