Base32 encode/decode for TypeScript.
npm install @niyari/base32-tssh
npm i @niyari/base32-ts
`
Demo
https://niyari.github.io/base32-ts/demo/
Supported Browsers
ECMAScript 2020 and later. (Using BigInt within Crockford.)
* Chrome 89+
* Firefox 87+
* Safari 14+
* Edge(Chromium) 89+
Usage
$3
`js
const base32 = new Base32();
let base32_encoded = base32.encode('foobar');
// str = "MZXW6YTBOI======"
let base32_decoded = base32.decode('MZXW6YTBOI======');
// str = "foobar"
`
$3
`js
const base32_hex = new Base32({ variant: 'hex' });
base32_hex.encode('foobar');
// str = "CPNMUOJ1E8======"
base32_hex.decode('CPNMUOJ1E8======');
// str = "foobar"
`
$3
`js
const base32_clockwork = new Base32({ variant: 'clockwork' }); // Clockwork (short name 'maki')
base32_clockwork.encode('foobar');
// str = "CSQPYRK1E8"
base32_clockwork.decode('CSQPYRK1E8');
// str = "foobar"
`
$3
`js
base32.encode('Tofu on Fire!📛'); // (📛 = Name Badge:for Japanese preschoolers.)
// str = "KRXWM5JAN5XCARTJOJSSD4E7SONQ===="
`
API(Options)
`
new Base32([{ [variant] [,padding] [,raw] [,checksum] [,split] }]);
`
$3
`
{ variant: '' }
`
* RFC4648
* 4648
* 3548
* (empty)
* RFC4648_HEX
* hex
* Clockwork Base32
* clockwork
* maki
* Crockford
* crockford
$3
`
{ padding: }
`
| | RFC4648 | HEX | Clockwork | Crockford
---: | :---: | :---: | :---: | :---:
| default | True | True | False | -
`js
const base32_np = new Base32({ padding: false }); // RFC4648 no padding
base32_np.encode('foobar');
// str = "MZXW6YTBOI"
const b32_cw_pad = new Base32({ variant: 'maki', padding: true }); // Clockwork use padding
b32_cw_pad.encode('foobar');
// str = "CSQPYRK1E8======"
`
$3
Return Uint8Array object.
`
{ raw: }
`
| | RFC4648 | HEX | Clockwork | Crockford
---: | :---: | :---: | :---: | :---:
| default | False | False | False | False(hexadecimal string)
`js
const base32 = new Base32();
base32.decode('MZXW6YTBOI======'); // (default)
const base32_raw0 = new Base32({ raw: false });
base32_raw0.decode('MZXW6YTBOI======');
// Return value: String
const base32_raw1 = new Base32({ raw: true });
base32_raw1.decode('MZXW6YTBOI======');
const base32_crockford_raw = new Base32({ variant: 'crockford', raw: true });
base32_crockford_raw.decode(123456);
// Return value: Uint8Array object
`
Crockford
Encode an integer into a Crockford Symbol string.
$3
`js
const base32_crockford = new Base32({ variant: 'crockford' });
base32_crockford.encode(1234);
// str = "16J"
base32_crockford.decode('16J');
// str = "0x04d2" = 1234
`
In decoding, the misleading character "IiLl" is treated as 1 and "Oo" is treated as 0.
`js
base32_crockford.decode('IiLl10Oo');
// str = "0x842108000"
`
API(Options)
`
{ variant: 'crockford' }
`
$3
`
{ variant: 'crockford', checksum: }
`
| | RFC4648 | HEX | Clockwork | Crockford
---: | :---: | :---: | :---: | :---:
| default | - | - | - | False
`js
const base32_crockford = new Base32({ variant: 'crockford', checksum: true });
base32_crockford.encode(1234);
// str = "16JD"
base32_crockford.decode('16JD');
// str = "0x04d2" = 1234
`
$3
`
{ variant: 'crockford', split: }
`
| | RFC4648 | HEX | Clockwork | Crockford
---: | :---: | :---: | :---: | :---:
| default | - | - | - | 0
`js
const base32_crockford = new Base32({ variant: 'crockford', split: 2 });
base32_crockford.encode(123456);
// str = "3R-J0"
const base32_crockford_s1 = new Base32({ variant: 'crockford', split: 1 });
base32_crockford_s1.encode(123456);
// str = "3-R-J-0"
``