Skip to content

Instantly share code, notes, and snippets.

@sasknot
Created November 18, 2021 19:56
Show Gist options
  • Save sasknot/d589d6dee833689e3f763e3216db4113 to your computer and use it in GitHub Desktop.
Save sasknot/d589d6dee833689e3f763e3216db4113 to your computer and use it in GitHub Desktop.
Get X.509 Certificate SHA-1 Thumbprint (nodejs)
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