Last active
June 29, 2021 19:27
-
-
Save ezekg/e88912ca01a6cd244df6ab712db47545 to your computer and use it in GitHub Desktop.
Encoding a hexidecimal Ed25519 verify key to DER format using Node.
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
const crypto = require('crypto') | |
{ | |
const verifyKey = 'e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788' | |
const licenseKey = 'key/Njg1ZjkwMDY5OGZkOWNiZTYwODRlNmRhZDdmMGU3NjI=.V0MIuqrUfTnPrCAPqi8YfnpGHNluoGs1QG0XqZ67D8q9dQvtuEaKVz_7w5cZkcYvOkNbvE28w8P4xVemXbBqAA==' | |
// Encode hexidecimal key into DER format (Node doesn't accept hex format) | |
const oid = Buffer.from([0x06, 0x03, 0x2B, 0x65, 0x70]) | |
const key = Buffer.from(verifyKey, 'hex') | |
const elements = Buffer.concat([ | |
Buffer.concat([ | |
Buffer.from([0x30]), // Sequence tag | |
Buffer.from([oid.length]), | |
oid, | |
]), | |
Buffer.concat([ | |
Buffer.from([0x03]), // Bit tag | |
Buffer.from([key.length + 1]), | |
Buffer.from([0x00]), // Zero bit | |
key, | |
]), | |
]) | |
const der = Buffer.concat([ | |
Buffer.from([0x30]), // Sequence tag | |
Buffer.from([elements.length]), | |
elements, | |
]) | |
// Initialize our public key | |
const publicKey = crypto.createPublicKey({ key: der, type: 'spki', format: 'der' }) | |
// Decode and verify the license key | |
const [signingData, encodedSignature] = licenseKey.split('.') | |
const [signingPrefix, encodedData] = signingData.split('/') | |
const signature = Buffer.from(encodedSignature, 'base64') | |
const data = Buffer.from(encodedData, 'base64').toString() | |
const ok = crypto.verify(null, Buffer.from(signingData), publicKey, signature) | |
console.log({ ok, data }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment