79628873

Date: 2025-05-19 13:54:55
Score: 4.5
Natty:
Report link

Answering my own question:

Thanks to @bestbeforetoday's comment, I managed to rewrite the signing code and now it looks like this, for anyone having the same problem as I had:

function getPrivKey(pemFile) {
  const pemContent = fs.readFileSync(pemFile, 'utf8');
  const key = crypto.createPrivateKey(pemContent);
  const jwk = key.export({ format: 'jwk' });
  const d = jwk.d; 
  return Buffer.from(d, 'base64url'); 
}

function fabricSign(message, privateKeyPemFile) {
  const privateKeyBytes = getPrivKey(privateKeyPemFile);
  const msgHash = crypto.createHash('sha256').update(message).digest();
  const signature = p256.sign(msgHash, privateKeyBytes);
  const signaturep1 = signature.normalizeS();
  const signaturep2 = signaturep1.toDERRawBytes();
  return signaturep2;
}
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Me too answer (2.5): having the same problem
  • User mentioned (1): @bestbeforetoday's
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Eduardo Valente