This is a binary and text encoding tool that uses radix-85 representation.
It's like base64, only that it uses 85 symbols rather than 64, which produces smaller results. This is also different from base64 because it provides encryption.
It can be used from the command line or programatically as a node module in any Node.js® script.
Although I strong recommend reading the whole document, if you're in a rush to start using this package, or just need a quick sample code to start, jump straight to the Examples section. Though, even is the case, you might also like to take a look at the Using as a CLI tool section.
Check out the time to encode and decode 1GB of data in an AMD Ryzen 9 3900X:
Format | Encoding | Decoding |
---|---|---|
Binary | 1038ms | 403ms |
String | 971ms | 853ms |
If you want to use r85 directly from the command line, install it globally with
npm install -g r85
and run r85 --help
for usage.
Usage: r85 [OPTIONS]... [FILE]
Encode or decode FILE or stdin to stdout by default.
Options:
-d, --decode decodes FILE (default if file has .r85 extension)
-e, --encode encodes FILE (default if file doesn't have .r85 extension)
-o, --out=FILE writes to FILE
-k, --key=KEY a key to encrypt/decrypt the data
-K, --key same as -k, but read key from stdin
and cannot be used together with [FILE]
-f, --force overwrites output FILE (--out or -o) if it exists
-h, --help print this help
With no [FILE], reads stdin.
With no [FILE] and with --key (or -K), key is first line from stdin.
With no --out=FILE (or -o FILE), writes to stdout.
Obviously r85 can be also be just required in any node module to be used programatically.
Usually these would be the steps to encode/decode data:
- require r85
- create an instance
- call one of the four methods: encode, encodeToString, decode, decodeToString
The encode
method accepts Array, TypedArray, Buffer and String as argument. It
always return a Buffer with the encoded content.
Similarly, the decode
method accepts Array, TypedArray, Buffer and String as
argument. It also always return a Buffer with the decoded content.
The special methods encodeToString
and decodeToString
also accept Array,
TypedArray, Buffer and String as argument. However they return a String rather
than a Buffer. That might be convenient in some situation.
const fs = require('fs');
const R85 = require('r85');
const r85 = new R85();
const buffer = fs.readFileSync('file.bin');
const encodedBuffer = r85.encode(buffer);
const decodedBuffer = r85.decode(decodedBuffer);
fs.writeFileSync('file.bin.r85', encodedBuffer);
// file.bin.copy has the same content as file.bin
fs.writeFileSync('file.bin.copy', decodedBuffer);
const R85 = require('r85');
const r85 = new R85();
const euroSymbolUTF8Bytes = [0xE2, 0x82, 0xAC];
const encodedBuffer = r85.encode(euroSymbolUTF8Bytes);
const encodedArray = Array.from(euroSymbolEncodedBuffer);
const encodedString = String.fromCharCode.apply(null, encodedArray);
// logs [ 51, 101, 67, 51 ]
console.log(encodedArray);
// logs 3eC3
console.log(encodedString);
const R85 = require('r85');
const r85 = new R85();
const euroSymbol = '€'; // this is 2-byte in UCS2 \u20AC
const encodedString = r85.encodeToString(euroSymbol);
const decodedString = r85.decodeToString(encodedString);
// logs 3eC3
console.log(encodedString);
// logs €
console.log(decodedString);
In order to encode data with encryption, or decode previously encryted encoded data, provide a key as argument to the constructor.
const R85 = require('r85');
const r85 = new R85('s3cret');
const euroSymbol = '€'; // this is 2-byte in UCS2 \u20AC
const encodedString = r85.encodeToString(euroSymbol);
const decodedString = r85.decodeToString(encodedString);
// logs XncX
console.log(encodedString);
// logs €
console.log(decodedString);
Willian Balmant |