-
Notifications
You must be signed in to change notification settings - Fork 16
Closed
Description
@sjudson, thanks for writing this library!
A couple thoughts from a new user of your library:
- You may want to remove the example code that doesn't set up secret key material.
- I was surprised that the
.injectmethod 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...) - I didn't see a
.decodeexample 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);
}
})();neodon
Metadata
Metadata
Assignees
Labels
No labels