UNPKG

multiformats

Version:

Interface for multihash, multicodec, multibase and CID

93 lines (88 loc) 2.1 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var base = require('./base.js'); function decode(input, alphabet) { input = input.replace(new RegExp('=', 'g'), ''); const length = input.length; let bits = 0; let value = 0; let index = 0; const output = new Uint8Array(length * 5 / 8 | 0); for (let i = 0; i < length; i++) { value = value << 5 | alphabet.indexOf(input[i]); bits += 5; if (bits >= 8) { output[index++] = value >>> bits - 8 & 255; bits -= 8; } } return output; } function encode(buffer, alphabet) { const length = buffer.byteLength; const view = new Uint8Array(buffer); const padding = alphabet.indexOf('=') === alphabet.length - 1; if (padding) { alphabet = alphabet.substring(0, alphabet.length - 1); } let bits = 0; let value = 0; let output = ''; for (let i = 0; i < length; i++) { value = value << 8 | view[i]; bits += 8; while (bits >= 5) { output += alphabet[value >>> bits - 5 & 31]; bits -= 5; } } if (bits > 0) { output += alphabet[value << 5 - bits & 31]; } if (padding) { while (output.length % 8 !== 0) { output += '='; } } return output; } const base32 = base.withAlphabet({ prefix: 'b', name: 'base32', alphabet: 'abcdefghijklmnopqrstuvwxyz234567', encode, decode }); const base32pad = base.withAlphabet({ prefix: 'c', name: 'base32pad', alphabet: 'abcdefghijklmnopqrstuvwxyz234567=', encode, decode }); const base32hex = base.withAlphabet({ prefix: 'v', name: 'base32hex', alphabet: '0123456789abcdefghijklmnopqrstuv', encode, decode }); const base32hexpad = base.withAlphabet({ prefix: 't', name: 'base32hexpad', alphabet: '0123456789abcdefghijklmnopqrstuv=', encode, decode }); const base32z = base.withAlphabet({ prefix: 'h', name: 'base32z', alphabet: 'ybndrfg8ejkmcpqxot1uwisza345h769', encode, decode }); exports.base32 = base32; exports.base32hex = base32hex; exports.base32hexpad = base32hexpad; exports.base32pad = base32pad; exports.base32z = base32z;