Created
November 18, 2021 19:56
-
-
Save sasknot/d589d6dee833689e3f763e3216db4113 to your computer and use it in GitHub Desktop.
Get X.509 Certificate SHA-1 Thumbprint (nodejs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import crypto from 'crypto' | |
const x509Certificate = '[insert certificate here]' | |
const certificateStrings = x509Certificate.match(/-----BEGIN CERTIFICATE-----\s*([\s\S]+?)\s*-----END CERTIFICATE-----/i) | |
if (certificateStrings && certificateStrings[1]) { | |
throw new Error('Invalid certificate') | |
} | |
const sha1Thumbprint = crypto | |
.createHash('sha1') | |
.update(Buffer.from(certificateStrings[1], 'base64')) | |
.digest('hex') | |
.toUpperCase() | |
const base64UrlSha1Thumbprint = Buffer.from(sha1Thumbprint, 'hex').toString('base64url') | |
console.log('SHA-1 Thumbprint', sha1Thumbprint) | |
console.log('SHA-1 Thumbprint Base64Url', base64UrlSha1Thumbprint) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment