Advertisement
Guest User

Base32 Encoder/Decoder

a guest
Sep 26th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. const BASE32 = '0123456789abcdefghijklmnopqrstuv';
  4. const BASE32_REVERSE = new Uint8Array(256);
  5. for (let i = 0; i < BASE32.length; i++) {
  6.     BASE32_REVERSE[BASE32.charCodeAt(i)] = i;
  7. }
  8.  
  9. class cBase32EncoderDecoder{
  10.  
  11.     static fEncode = (aByteArray) => {
  12.         let iLength = aByteArray.length;
  13.         let sBase32 = '';
  14.  
  15.         for (let i = 0; i < iLength; i += 5) {
  16.             sBase32 += BASE32[aByteArray[i] >> 3];
  17.             sBase32 += BASE32[((aByteArray[i] & 7) << 2) | (aByteArray[i + 1] >> 6)];
  18.             sBase32 += BASE32[((aByteArray[i + 1] & 63) >> 1)];
  19.             sBase32 += BASE32[((aByteArray[i + 1] & 1) << 4 | (aByteArray[i + 2]) >> 4)];
  20.             sBase32 += BASE32[((aByteArray[i + 2] & 15) << 1 | (aByteArray[i + 3]) >> 7)];
  21.             sBase32 += BASE32[((aByteArray[i + 3] & 127) >> 2)];
  22.             sBase32 += BASE32[((aByteArray[i + 3] & 3) << 3 | (aByteArray[i + 4]) >> 5)];
  23.             sBase32 += BASE32[((aByteArray[i + 4] & 31))];
  24.         }
  25.  
  26.         if (iLength % 5 === 4) {
  27.             sBase32 = sBase32.substring(0, sBase32.length - 1);
  28.         }
  29.         else if (iLength % 5 === 3) {
  30.             sBase32 = sBase32.substring(0, sBase32.length - 3);
  31.         }
  32.         else if (iLength % 5 === 2) {
  33.             sBase32 = sBase32.substring(0, sBase32.length - 4);
  34.         }
  35.         else if (iLength % 5 === 1) {
  36.             sBase32 = sBase32.substring(0, sBase32.length - 6);
  37.         }
  38.  
  39.         return sBase32;
  40.     };
  41.  
  42.     static fDecode = (sBase32) => {
  43.         let iBufferLength = sBase32.length * 0.625;
  44.         let iLength = sBase32.length;
  45.        
  46.         let iEncoded1;
  47.         let iEncoded2;
  48.         let iEncoded3;
  49.         let iEncoded4;
  50.         let iEncoded5;
  51.         let iEncoded6;
  52.         let iEncoded7;
  53.         let iEncoded8;
  54.  
  55.         let aByteArray = new Uint8Array(iBufferLength);
  56.        
  57.         let p = 0;
  58.         for (let i = 0; i < iLength; i += 8) {
  59.             iEncoded1 = BASE32_REVERSE[sBase32.charCodeAt(i)];
  60.             iEncoded2 = BASE32_REVERSE[sBase32.charCodeAt(i + 1)];
  61.             iEncoded3 = BASE32_REVERSE[sBase32.charCodeAt(i + 2)];
  62.             iEncoded4 = BASE32_REVERSE[sBase32.charCodeAt(i + 3)];
  63.             iEncoded5 = BASE32_REVERSE[sBase32.charCodeAt(i + 4)];
  64.             iEncoded6 = BASE32_REVERSE[sBase32.charCodeAt(i + 5)];
  65.             iEncoded7 = BASE32_REVERSE[sBase32.charCodeAt(i + 6)];
  66.             iEncoded8 = BASE32_REVERSE[sBase32.charCodeAt(i + 7)];
  67.  
  68.             aByteArray[p++] = (iEncoded1 << 3) | (iEncoded2 >> 2);
  69.             aByteArray[p++] = ((iEncoded2 & 3) << 6) | (iEncoded3 << 1) | (iEncoded4 >> 4);
  70.             aByteArray[p++] = ((iEncoded4 & 15) << 4) | (iEncoded5 >> 1);
  71.             aByteArray[p++] = ((iEncoded5 & 1) << 7) | (iEncoded6 << 2) | (iEncoded7 >> 3);
  72.             aByteArray[p++] = ((iEncoded7 & 7) << 5) | iEncoded8;
  73.         }
  74.  
  75.         return aByteArray;
  76.     };
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement