Skip to content

Commit

Permalink
refactor: update getSPKI to use crypto.createPublicKey when available
Browse files Browse the repository at this point in the history
fixes #752
  • Loading branch information
panva committed Feb 22, 2025
1 parent f3ba4c7 commit 92392a0
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/lib/asn1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,18 @@ function spkiFromX509(buf: Uint8Array) {
return encodeBase64(tbsCertificate[tbsCertificate[0].raw[0] === 0xa0 ? 6 : 5].raw)
}

let X509Certificate: any
let createPublicKey: any
function getSPKI(x509: string): string {
if (
try {
// @ts-ignore
(X509Certificate ||= globalThis.process?.getBuiltinModule?.('node:crypto')?.X509Certificate)
) {
createPublicKey ??= globalThis.process?.getBuiltinModule?.('node:crypto')?.createPublicKey
} catch {
createPublicKey = 0
}

if (createPublicKey) {
try {
return new X509Certificate(x509).publicKey.export({ format: 'pem', type: 'spki' })
return new createPublicKey(x509).export({ format: 'pem', type: 'spki' })
} catch {}
}
const pem = x509.replace(/(?:-----(?:BEGIN|END) CERTIFICATE-----|\s)/g, '')
Expand Down

0 comments on commit 92392a0

Please sign in to comment.