0

I have a PASERK key like

k4.secret.5xxxxxxxpA

How can I obtain a crypto.KeyObject from that string?

RubenLaguna
  • 21,435
  • 13
  • 113
  • 151

1 Answers1

0

You can just

V4.bytesToKeyObject(Buffer.from("k4.secret.5xxxxxxx".slice(10),'base64url'))

and that will return a crypto.PrivateKeyObject

I just generated a new disposable ed25519 keypair to demonstrate:

const {V4} = require('paseto');
const crypto = require('crypto');
const fs = require('fs');

(async () => {
    const {publicKey: publicKey_paserk,secretKey: privateKey_paserk} = await V4.generateKey('public', {format: "paserk"}) // strings
    console.log(privateKey_paserk)    
    console.log(publicKey_paserk)
})();

The resulting PASERK keypair (don't use this keypair anywhere, generate your own):

k4.secret.UN8Rb689EWZyjyLfoDj6YKuo-YgfVBf-uDNKn8O9L7KUM_IjgIzaTC8Gd5ar1finBngBJpvGb1_cDyElvKxnAg
k4.public.lDPyI4CM2kwvBneWq9X4pwZ4ASabxm9f3A8hJbysZwI

You can see how those PASERK key strings are converted to PrivateKeyObject/PublicKeyObject below:

const {V4} = require('paseto');
const crypto = require('crypto');
const fs = require('fs');

(async () => {
    // convert from PASERK to back to crypto.PrivateKeyObject / crypto.PublicKeyObject
    const privateKeyObject = V4.bytesToKeyObject(Buffer.from("k4.secret.9zTT6GVvrbWq8g5u5BSR2NfycLAQn8pVuguTHMjR-Zwtgq6obELzSPMny62hlxMhe-KCQMv73hNVlY19wZlXPA".slice(10),'base64url'))
    const publicKeyObject = V4.bytesToKeyObject(Buffer.from("k4.public.LYKuqGxC80jzJ8utoZcTIXvigkDL-94TVZWNfcGZVzw".slice(10),'base64url'))

    console.log(privateKeyObject.export({type: 'pkcs8', format: 'pem'}))
    console.log(publicKeyObject.export({type: 'spki', format: 'pem'}))
})();

which produces the output:

-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIPc00+hlb621qvIObuQUkdjX8nCwEJ/KVboLkxzI0fmc
-----END PRIVATE KEY-----

-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEALYKuqGxC80jzJ8utoZcTIXvigkDL+94TVZWNfcGZVzw=
-----END PUBLIC KEY-----
RubenLaguna
  • 21,435
  • 13
  • 113
  • 151