Advertisement
vikasg603

Untitled

Nov 14th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. public class base32 {
  2.     private static final String base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  3.     private static final int[] base32Lookup =
  4.     { 0xFF,0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
  5.       0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  6.       0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
  7.       0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,
  8.       0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
  9.       0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
  10.       0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
  11.       0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,
  12.       0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
  13.       0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF  
  14.     }; //base32 binary codes
  15.  
  16.     static public String encode(final byte[] bytes) {
  17.         int i = 0, index = 0, digit = 0;
  18.         int currByte, nextByte;
  19.         StringBuffer base32 = new StringBuffer((bytes.length + 7) * 8 / 5);
  20.  
  21.         while (i < bytes.length) {
  22.             currByte = (bytes[i] >= 0) ? bytes[i] : (bytes[i] + 256);
  23.  
  24.             /* Is the current digit going to span a byte boundary? */
  25.             if (index > 3) {
  26.                 if ((i + 1) < bytes.length) {
  27.                     nextByte = (bytes[i + 1] >= 0)
  28.                        ? bytes[i + 1] : (bytes[i + 1] + 256);
  29.                 } else {
  30.                     nextByte = 0;
  31.                 }
  32.  
  33.                 digit = currByte & (0xFF >> index);
  34.                 index = (index + 5) % 8;
  35.                 digit <<= index;
  36.                 digit |= nextByte >> (8 - index);
  37.                 i++;
  38.             } else {
  39.                 digit = (currByte >> (8 - (index + 5))) & 0x1F;
  40.                 index = (index + 5) % 8;
  41.                 if (index == 0)
  42.                     i++;
  43.             }
  44.             base32.append(base32Chars.charAt(digit));
  45.         }
  46.  
  47.         return base32.toString();
  48.     }
  49.  
  50.     static public byte[] decode(final String base32) {
  51.         int i, index, lookup, offset, digit;
  52.         byte[] bytes = new byte[base32.length() * 5 / 8];
  53.  
  54.         for (i = 0, index = 0, offset = 0; i < base32.length(); i++) {
  55.             lookup = base32.charAt(i) - '0';
  56.  
  57.             /* Skip chars outside the lookup table */
  58.             if (lookup < 0 || lookup >= base32Lookup.length) {
  59.                 continue;
  60.             }
  61.  
  62.             digit = base32Lookup[lookup];
  63.  
  64.             /* If this digit is not in the table, ignore it */
  65.             if (digit == 0xFF) {
  66.                 continue;
  67.             }
  68.  
  69.             if (index <= 3) {
  70.                 index = (index + 5) % 8;
  71.                 if (index == 0) {
  72.                     bytes[offset] |= digit;
  73.                     offset++;
  74.                     if (offset >= bytes.length)
  75.                         break;
  76.                 } else {
  77.                     bytes[offset] |= digit << (8 - index);
  78.                 }
  79.             } else {
  80.                 index = (index + 5) % 8;
  81.                 bytes[offset] |= (digit >>> index);
  82.                 offset++;
  83.  
  84.                 if (offset >= bytes.length) {
  85.                     break;
  86.                 }
  87.                 bytes[offset] |= digit << (8 - index);
  88.             }
  89.         }
  90.         return bytes;
  91.     }
  92.  
  93.     static public void main(String[] args) {
  94.         java.util.Scanner in = new java.util.Scanner(System.in);
  95.  
  96.         System.out.println("======= Program for base32 hashing =======");
  97.         System.out.println();
  98.  
  99.         System.out.println("1. Encoding");
  100.         System.out.println("2. Decoding");
  101.         System.out.println("3. Exit");
  102.         System.out.print("Option : ");
  103.  
  104.         int op = in.nextInt();
  105.         String input,encode,decode;
  106.         switch(op)
  107.         {
  108.             case 1 :
  109.                 System.out.print("Enter byte code to encode it : ");
  110.                 in.nextLine(); //Buffering the input
  111.                 input = in.nextLine();
  112.  
  113.                 encode = base32.encode(input.getBytes()); // sending paramter as bytes
  114.  
  115.                 System.out.println("Encoded value : " + encode);
  116.                 break;
  117.  
  118.             case 2 :
  119.                 System.out.print("Enter string to decode it : ");
  120.                 in.nextLine();
  121.                 input = in.nextLine();
  122.  
  123.                 decode = new String(base32.decode(input)); // sending paramter as String and then converting output to String
  124.  
  125.                 System.out.println("Decoded value : " + decode);
  126.                 break;
  127.             case 3 :
  128.                 return;
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement