@darkwolf/base32

1.0.1 • Public • Published

Base32

Install

npm i --save @darkwolf/base32

Usage

// ECMAScript
import Base32 from '@darkwolf/base32'
// CommonJS
const Base32 = require('@darkwolf/base32')

// Number Encoding
const integer = Number.MAX_SAFE_INTEGER // => 9007199254740991
const encodedInt = Base32.encodeInt(integer) // => 'H7777777777'
const decodedInt = Base32.decodeInt(encodedInt) // => 9007199254740991

const negativeInteger = -integer // => -9007199254740991
const encodedNegativeInt = Base32.encodeInt(negativeInteger) // => '-H7777777777'
const decodedNegativeInt = Base32.decodeInt(encodedNegativeInt) // => -9007199254740991

// BigInt Encoding
const bigInt = BigInt(Number.MAX_VALUE) // => 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368n
const encodedBigInt = Base32.encodeBigInt(bigInt) // => 'P7777777776AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
const decodedBigInt = Base32.decodeBigInt(encodedBigInt) // => 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368n

const negativeBigInt = -bigInt // => -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368n
const encodedNegativeBigInt = Base32.encodeBigInt(negativeBigInt) // => '-P7777777776AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
const decodedNegativeBigInt = Base32.decodeBigInt(encodedNegativeBigInt) // => -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368n

// Text Encoding
const text = 'Ave, Darkwolf!'
const encodedText = Base32.encodeText(text) // => 'IF3GKLBAIRQXE23XN5WGMII='
const decodedText = Base32.decodeText(encodedText) // => 'Ave, Darkwolf!'

const emojis = '🐺🐺🐺'
const encodedEmojis = Base32.encodeText(emojis) // => '6CPZBOXQT6ILV4E7SC5A===='
const decodedEmojis = Base32.decodeText(encodedEmojis) // => '🐺🐺🐺'

// Buffer Encoding
const buffer = Uint8Array.of(0x00, 0x02, 0x04, 0x08, 0x0f, 0x1f, 0x3f, 0x7f, 0xff) // => <Uint8Array 00 02 04 08 0f 1f 3f 7f ff>
const encodedBuffer = Base32.encode(buffer) // => <Uint8Array 41 41 42 41 49 43 41 50 44 34 37 58 37 37 59>
const decodedBuffer = Base32.decode(encodedBuffer) // => <Uint8Array 00 02 04 08 0f 1f 3f 7f ff>

const encodedBufferToString = Base32.encodeToString(buffer) // => 'AABAICAPD47X77Y='
const decodedBufferFromString = Base32.decodeFromString(encodedBufferToString) // => <Uint8Array 00 02 04 08 0f 1f 3f 7f ff>

// Custom Alphabet
const base32 = new Base32('234567ABCDEFGHIJKLMNOPQRSTUVWXYZ')

const encInt = base32.encodeInt(integer) // => 'BZZZZZZZZZZ'
const decInt = base32.decodeInt(encInt) // => 9007199254740991

const encNegativeInt = base32.encodeInt(negativeInteger) // => '-BZZZZZZZZZZ'
const decNegativeInt = base32.decodeInt(encNegativeInt) // => -9007199254740991

const encBigInt = base32.encodeBigInt(bigInt) // 'JZZZZZZZZZY22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222'
const decBigInt = base32.decodeBigInt(encBigInt) // => 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368n

const encNegativeBigInt = base32.encodeBigInt(negativeBigInt) // => '-JZZZZZZZZZY22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222'
const decNegativeBigInt = base32.decodeBigInt(encNegativeBigInt) // => -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368n

const encText = base32.encodeText(text) // => 'C7VAEF32CLKR6UVRHXQAGCC='
const decText = base32.decodeText(encText) // => 'Ave, Darkwolf!'

const encEmojis = base32.encodeText(emojis) // => 'Y4JT3IRKNYCFPW6ZM4X2===='
const decEmojis = base32.decodeText(encEmojis) // => '🐺🐺🐺'

const encBuffer = base32.encode(buffer) // => <Uint8Array 32 32 33 32 43 34 32 4a 35 57 5a 52 5a 5a 53>
const decBuffer = base32.decode(encBuffer) // => <Uint8Array 00 02 04 08 0f 1f 3f 7f ff>

const encBufferToString = base32.encodeToString(buffer) // => '2232C42J5WZRZZS='
const decBufferFromString = base32.decodeFromString(encBufferToString) // => <Uint8Array 00 02 04 08 0f 1f 3f 7f ff>

API Documentation

Contact Me

GitHub: @PavelWolfDark

Telegram: @PavelWolfDark

Email: PavelWolfDark@gmail.com

Readme

Keywords

Package Sidebar

Install

npm i @darkwolf/base32

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

38.7 kB

Total Files

6

Last publish

Collaborators

  • pavelwolfdark