PeculiarVentures / x509

@peculiar/x509 is an easy to use TypeScript/Javascript library based on @peculiar/asn1-schema that makes generating X.509 Certificates and Certificate Requests as well as validating certificate chains easy
https://peculiarventures.github.io/x509/
MIT License
89 stars 15 forks source link

X509Certificate fingerprint #79

Open StebeletskyiMyroslav opened 5 months ago

StebeletskyiMyroslav commented 5 months ago

Hiow can i get formatted fingerprint of certificate ?

Foe example i have following certificate:

-----BEGIN CERTIFICATE----- MIICxDCCAaygAwIBAgIIBKsgcOucPtIwDQYJKoZIhvcNAQELBQAwIjEgMB4GA1UE AxMXUG9ydG5veCBTU08gQ2VydGlmaWNhdGUwHhcNMjQwNjExMTY1MjIzWhcNMjcw NjExMTY1MjIzWjAiMSAwHgYDVQQDExdQb3J0bm94IFNTTyBDZXJ0aWZpY2F0ZTCC ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMNqit0JSoscHlrO+ggUpww1 bmv1XF7Qz2y/VFxPNSKE9WMAYbdYlV9m5ZF0dpPdozvbDYYr60H6s0FJYt42951r NGFKI+EwQs7kTv+LgIvXPTvI0CKnO+ULzDtjeE618ouKKLmULJBilr5THnngrjdd jTLd1jYUlVa2w9C7A1RZeBwJVxG8jTbfVzZReVir7Iu0k1nxxjiOuvSj1oqfH+NT mLthwXBuYpSH0wteU2TcXyJYOCkmYklWVB5d4z0W6t8G8i+GmIpnoA9db7NYDmbj iMyxgv0DSWLWHdtbDyDgP/8n9X5uTxdvcADK1iE1vAlK1dbibf9Pw2bt7yLKUZcC AwEAATANBgkqhkiG9w0BAQsFAAOCAQEAocTLQoE+ERTNES36lykJXq05ryiBMDda xlEvSsOzbRWxRPPZgBHlMYlv+CKQGyM3nv2VClGVMbx5TdVB7NkrxbmFJoNxjjyk HkZa0lvyYxDgJVsCj2l9az1jFCU9KGJ++LAeCVwgrTBgFODAUYBA3ErdDgYKI2P9 C4HHf2ytQpBX1201NCHULZG+bduJIiKW2ourCfulppbM1M4AwSbsDw+efyKd9Gn7 4wJiQFiYXsgKkWnb39Cs9WKJi6DGyJV2/cQ9pR8y3KBz4WIddRmj6VAUBrvKvOR5 vpM5dy8b/U5Bsb4SO5S0Idx69+/tko/WUgxIUyhIXPHFh4+AE3+uaA== -----END CERTIFICATE-----

I try to use some online services that detect certificate fingerprint, and what i have with mentioned above certificate: sha-1: eab12636cfe4526d0c2c3a615ac73fc0689e2d8d.

I'm just asking how can i do the same with @peculiar/x509 library.

StebeletskyiMyroslav commented 5 months ago

And yes, i know about getThumbprint method. It returns array of bytes. But the problem occurs when i'm trying co convert this value to some hash similar value. i'm just getting some strange symbols. It would be nice if you provide me an example how can i convert (decode) the result of getThumbprint method to hash string.

StebeletskyiMyroslav commented 5 months ago

If someone wondering how to do it, here is solution for browser environment:

const buf2hex = (buffer: ArrayBuffer): string => { return [...new Uint8Array(buffer)] .map(x => x.toString(16).padStart(2, '0')) .join(''); }

trasherdk commented 5 months ago

The solution from #34

const cert = new x509.X509Certificate(pem);
const thumbprint = await cert.getThumbprint();
const formattedThumbprint: string[] = [];
for (const octet of Buffer.from(thumbprint)) {
  formattedThumbprint.push(`${octet.toString(16)}`.padStart(2, "0").toUpperCase());
}
console.log(formattedThumbprint.join(":")); // 10:B9:24:B0:18:57:23:F2:2D:EB:FD:94:37:55:85:E9:CF:1D:8B:93