
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
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 the library using Yarn or npm.
$ yarn add branca
$ npm install branca
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);
/*
TYfc6x7g8HiQf9HMkPwXC33UKwESCiBHrnVbb6AjDTaRR5oDxt3bK8kyiEyyc8HDqfnukQlMHT
*/
const payload = branca.decode(token);
console.log(payload.toString());
/* tuupola@appelsiini.net */
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);
/*
3Gq57osRXk7UsZsqzLuLOoHYj2VgrGvhkETjZ4J1ftW7zhALYFUol2jDyxYtmrqJfi5DbKx7BqIptfeaoN2yadmJxSIx
*/
const payload = JSON.parse(branca.decode(token));
console.log(payload);
/* { scope: [ 'read', 'write', 'delete' ] } */
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);
/*
2EZpow8Nwk6Z9UxMel3kzFUe5boHV480zwkZDp6hNgaatnOCt4YbqgCRICKnm7IfJgxzQpT9eYdrTzyb
*/
const binary = branca.decode(token);
const payload = msgpack.decode(Buffer.from(binary));
console.log(payload);
/* { scope: [ 'read', 'write', 'delete' ] } */
You can run tests manually with the following command.
$ node test.js
Please see CONTRIBUTING for details.
If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
FAQs
Authenticated and encrypted API tokens using modern crypto
The npm package branca receives a total of 413 weekly downloads. As such, branca popularity was classified as not popular.
We found that branca demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.