sjudson / paseto.js

PASETO: Platform-Agnostic Security Tokens
MIT License
269 stars 16 forks source link

Loading base64 encoded key fails #37

Closed rustyconover closed 2 years ago

rustyconover commented 2 years ago

I'm trying to load in a base64 encoded symmetric key using this code:

   const encoderKey = new Paseto.SymmetricKey(new Paseto.V2());
    // await encoderKey.generate();
    await encoderKey.base64(encryptionKey);
    const encoder = new Paseto.V2();

Returns in an error:

   TypeError: Cannot read property '_malloc' of undefined

      at d (node_modules/libsodium-wrappers-sumo/dist/modules-sumo/libsodium-wrappers.js:1:17462)
      at new l (node_modules/libsodium-wrappers-sumo/dist/modules-sumo/libsodium-wrappers.js:1:17376)
      at Object.e.from_base64 (node_modules/libsodium-wrappers-sumo/dist/modules-sumo/libsodium-wrappers.js:1:78331)
      at Object.fromB64URLSafe (node_modules/paseto.js/lib/utils.js:288:29)
      at SymmetricKey.base64 (node_modules/paseto.js/lib/key/symmetric.js:73:28)

But if I call encoderKey.generate() before attempting to load the key everything works.

sjudson commented 2 years ago

@rustyconover try:

   const encoderKey = new Paseto.SymmetricKey(new Paseto.V2());
   await encoderKey.base64(encryptionKey);
   const encoder = encoderKey.protocol();

That last line doesn't work because you're generating a new protocol object, not loading the one attached to the key. If that doesn't work (especially because the error is on the await line) then I would guess your encryptionKey is not of the right type/format.

sjudson commented 2 years ago

Going to close this for now, feel free to reopen if the issue isn't resolved and you can provide more information.