1

I am using node-forge, and need to parse a CSR-PEM which can be done with the following method:

Forge.pki.certificationRequestFromPem(csr)

But now i need to get the SHA-256 Hash of the CSR and a MD5 Hash. I have gone through all the examples and documentation but cant find anything. Can anyone assist with this if they know forge well? Or maybe another nodejs package that can do it?

I just noticed that i actually need the hash from the DER-encoded CSR, so first i need to convert the PEM to DER :-(

PixelPaul
  • 1,022
  • 1
  • 10
  • 18

1 Answers1

0

You can get the CSR hashes in the following way:

const Forge = require('node-forge');

const csrPem = `-----BEGIN CERTIFICATE REQUEST-----
MIIDKjCCAhICAQAwgYQxFDASBgNVBAMMC2V4YW1wbGUuY29tMRQwEgYDVQQHDAtM
b3MgQW5nZWxlczETMBEGA1UECAwKQ2FsaWZvcm5pYTEUMBIGA1UECgwLRXhhbXBs
ZSBJbmMxCzAJBgNVBAsMAk5BMREwDwYJKoZIhvcNAQkBFgJOQTELMAkGA1UEBhMC
VVMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDwkcOjk1kyo7cJ6oJi
Eh8ziUh+35NdgUXrJWhpb0sWddFYaq+VlwXp1fE9luQRc151zr4lLdGyV26LYGfT
A85S40q9IcSXklB+gQG5O+wdRisI76HMnQ/SFoHKOjsuaH+vosZvWureifuiTqly
kGsw6N+4i+O8RB/vQl9y6y1oLjeUOXxiBQkU97e9GBzXzvwSyXkrujArchRhkpd0
K7US0lkNbfV3As1UQxxkGDWXRHNYKJgjmTOQ0clzwBTL54gcgSGtwxEoVHgzgOGi
13+YTDvxDFKbZGEMnIAe0vHefiRIjXGujF2sbA+tJHw9lmNTleWI3XCwguosYWuY
R8eFAgMBAAGgYDBeBgkqhkiG9w0BCQ4xUTBPMAkGA1UdEwQCMAAwCwYDVR0PBAQD
AgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAWBgNVHREEDzANggtl
eGFtcGxlLmNvbTANBgkqhkiG9w0BAQsFAAOCAQEAvGmMT1+5rJ7F5cVDKs551BnH
dXOCTrhJ9gQ/IlYsT9h5IowJzkkaYwRDDy3vuxYCg3GrexhFEHrwQq62X7U46Wrg
U5NIKbFoOIEW18mhhhgeHOBqyNlMmCwZgDD99+O6NRtHAr/hMW5xDjHdcmtJKh0w
aqYyiEpR8YUAqod4pDT20IGdlDksoGP5bqfeIjm3Xqqz5SFj2zZMMA0RdNOibxMU
64cYx+iJ4+tfDg3mHfoR2YIvPz/WhrT0/9iKfh2nGH0xZWge5A28zMaGmEVSrt2a
DSBRNlhZ9XQn3d1W3Om0DiTv9448RuCZsOJSj8iWaDvhCKzei+WaLznFiCKyNA==
-----END CERTIFICATE REQUEST-----`


const csrBytes = Forge.pki.pemToDer(csrPem).getBytes();

const md5 = Forge.md.md5.create();
const sha256 = Forge.md.sha256.create();

md5.update(csrBytes, 'raw')
sha256.update(csrBytes, 'raw')

console.log(md5.digest().toHex().toUpperCase());
console.log(sha256.digest().toHex().toUpperCase());
antonku
  • 7,377
  • 2
  • 15
  • 21