Branca
What?
Branca is a secure easy to use token format which makes it hard to shoot yourself in the foot. It uses IETF XChaCha20-Poly1305 AEAD symmetric encryption to create encrypted and tamperproof tokens. Payload itself is an arbitrary sequence of bytes. You can use for example a JSON object, plain text string or even binary data serialized by MessagePack or Protocol Buffers. It is possible to use Branca as an alternative to JWT.
Install
Install the library using Yarn or npm.
$ yarn add branca
$ npm install branca
Usage
Token payload can be any arbitrary data such as string containing an email
address.
const key = "supersecretkeyyoushouldnotcommit";
const branca = require("branca")(key);
const token = branca.encode("tuupola@appelsiini.net");
console.log(token);
const payload = branca.decode(token);
console.log(payload.toString());
Sometimes you might prefer JSON.
const key = "supersecretkeyyoushouldnotcommit";
const branca = require("branca")(key);
const json = JSON.stringify({"scope": ["read", "write", "delete"]});
const token = branca.encode(json);
console.log(token);
const payload = JSON.parse(branca.decode(token));
console.log(payload);
You can keep the token size small by using a space efficient serialization method such as MessagePack or Protocol Buffers.
const key = "supersecretkeyyoushouldnotcommit";
const branca = require("branca")(key);
const msgpack = require("msgpack5")();
const packed = msgpack.encode({"scope": ["read", "write", "delete"]});
const token = branca.encode(packed);
console.log(token);
const binary = branca.decode(token);
const payload = msgpack.decode(Buffer.from(binary));
console.log(payload);
Testing
You can run tests manually with the following command.
$ node test.js
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.