Skip to content

Add decode example to your README, perhaps? #5

@mceachen

Description

@mceachen

@sjudson, thanks for writing this library!

A couple thoughts from a new user of your library:

  1. You may want to remove the example code that doesn't set up secret key material.
  2. I was surprised that the .inject method returned undefined (and was a setter method). From the examples it looked like you were using a fluent API, but I missed the fact that your promise callback ignored the result from the promise. (I know, PEBCAK...)
  3. I didn't see a .decode example in the README.

Here's the little test script I wrote, feel free to include it in your README.

const Paseto = require("paseto.js");
const assert = require("assert");

(async () => {
  try {
    // This example uses symmetric key encryption, where the same private key is
    // used to both encrypt and decrypt.
    const sk = new Paseto.SymmetricKey(new Paseto.V2());

    // Secret keys must be at least 256 bits. 
    // Key material longer than 256 bits is ignored.
    const secretKey = Buffer.from(
      "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
      "hex"
    );

    // Provide your secret key material to SymmetricKey.
    // Note that `.inject` returns a Promise that you must `await` before continuing.
    await sk.inject(secretKey);

    // If you don't provide your private key before encoding, you'll get a runtime
    // error.

    const encoder = sk.protocol();

    // Payloads are either strings or Buffers. We're `JSON.stringify`ing our
    // payload object here just as an example:
    const payload = JSON.stringify({ payload: 123 });

    const encryptedToken = await encoder.encrypt(payload, sk);

    // "v2.local.khvgHKw7YcOsVwJ01epdAgBdB2gmrgrRQb8EbCsh7JBKpJqE5-Mp3-kRgWzfcRWRi1KvHkjDRA"
    console.dir({ encryptedToken });

    // Example decryption:
    const decryptedPayload = await encoder.decrypt(encryptedToken, sk);

    // Validate that the payload survived paseto-ization:
    assert(payload == decryptedPayload);

  } catch (err) {
    console.error("caught", err);
  }
})();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions