Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- const BASE32 = '0123456789abcdefghijklmnopqrstuv';
- const BASE32_REVERSE = new Uint8Array(256);
- for (let i = 0; i < BASE32.length; i++) {
- BASE32_REVERSE[BASE32.charCodeAt(i)] = i;
- }
- class cBase32EncoderDecoder{
- static fEncode = (aByteArray) => {
- let iLength = aByteArray.length;
- let sBase32 = '';
- for (let i = 0; i < iLength; i += 5) {
- sBase32 += BASE32[aByteArray[i] >> 3];
- sBase32 += BASE32[((aByteArray[i] & 7) << 2) | (aByteArray[i + 1] >> 6)];
- sBase32 += BASE32[((aByteArray[i + 1] & 63) >> 1)];
- sBase32 += BASE32[((aByteArray[i + 1] & 1) << 4 | (aByteArray[i + 2]) >> 4)];
- sBase32 += BASE32[((aByteArray[i + 2] & 15) << 1 | (aByteArray[i + 3]) >> 7)];
- sBase32 += BASE32[((aByteArray[i + 3] & 127) >> 2)];
- sBase32 += BASE32[((aByteArray[i + 3] & 3) << 3 | (aByteArray[i + 4]) >> 5)];
- sBase32 += BASE32[((aByteArray[i + 4] & 31))];
- }
- if (iLength % 5 === 4) {
- sBase32 = sBase32.substring(0, sBase32.length - 1);
- }
- else if (iLength % 5 === 3) {
- sBase32 = sBase32.substring(0, sBase32.length - 3);
- }
- else if (iLength % 5 === 2) {
- sBase32 = sBase32.substring(0, sBase32.length - 4);
- }
- else if (iLength % 5 === 1) {
- sBase32 = sBase32.substring(0, sBase32.length - 6);
- }
- return sBase32;
- };
- static fDecode = (sBase32) => {
- let iBufferLength = sBase32.length * 0.625;
- let iLength = sBase32.length;
- let iEncoded1;
- let iEncoded2;
- let iEncoded3;
- let iEncoded4;
- let iEncoded5;
- let iEncoded6;
- let iEncoded7;
- let iEncoded8;
- let aByteArray = new Uint8Array(iBufferLength);
- let p = 0;
- for (let i = 0; i < iLength; i += 8) {
- iEncoded1 = BASE32_REVERSE[sBase32.charCodeAt(i)];
- iEncoded2 = BASE32_REVERSE[sBase32.charCodeAt(i + 1)];
- iEncoded3 = BASE32_REVERSE[sBase32.charCodeAt(i + 2)];
- iEncoded4 = BASE32_REVERSE[sBase32.charCodeAt(i + 3)];
- iEncoded5 = BASE32_REVERSE[sBase32.charCodeAt(i + 4)];
- iEncoded6 = BASE32_REVERSE[sBase32.charCodeAt(i + 5)];
- iEncoded7 = BASE32_REVERSE[sBase32.charCodeAt(i + 6)];
- iEncoded8 = BASE32_REVERSE[sBase32.charCodeAt(i + 7)];
- aByteArray[p++] = (iEncoded1 << 3) | (iEncoded2 >> 2);
- aByteArray[p++] = ((iEncoded2 & 3) << 6) | (iEncoded3 << 1) | (iEncoded4 >> 4);
- aByteArray[p++] = ((iEncoded4 & 15) << 4) | (iEncoded5 >> 1);
- aByteArray[p++] = ((iEncoded5 & 1) << 7) | (iEncoded6 << 2) | (iEncoded7 >> 3);
- aByteArray[p++] = ((iEncoded7 & 7) << 5) | iEncoded8;
- }
- return aByteArray;
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement