UNPKG

@mark01/express-utils

Version:

npm package that contains utilities for express.js

44 lines (43 loc) 1.25 kB
export const b32ToBuf = (str) => { str = str.toUpperCase().replace(/=+$/, ''); const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; const buf = new ArrayBuffer(((str.length * 5) / 8) | 0); const arr = new Uint8Array(buf); let bits = 0; let value = 0; let index = 0; for (let i = 0; i < str.length; i++) { const idx = alphabet.indexOf(str[i]); if (idx === -1) throw new TypeError(`b32ToBuf: Invalid character found: ${str[i]}.`); value = (value << 5) | idx; bits += 5; if (bits >= 8) { arr[index++] = (value >>> (bits - 8)) & 255; bits -= 8; } } return buf; }; export const b32FromBuf = (buf) => { const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; const arr = new Uint8Array(buf); let bits = 0; let value = 0; let str = ''; for (let i = 0; i < arr.length; i++) { value = (value << 8) | arr[i]; bits += 8; while (bits >= 5) { str += alphabet[(value >>> (bits - 5)) & 31]; bits -= 5; } } if (bits > 0) { str += alphabet[(value << (5 - bits)) & 31]; } while (str.length % 8 !== 0) { str += '='; } return str; };