Base32 encoding/decoding class. : Base64 « Development Class « Java






Base32 encoding/decoding class.

        
/* (PD) 2001 The Bitzi Corporation
 * Please see http://bitzi.com/publicdomain for more info.
 *
 * Base32.java
 *
 * As modified by Jacob Davies:
 * 
 * Copyright  2010, Data Base Architects, Inc. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the names of Kalinda Software, DBA Software, Data Base Architects,
 *       Itemscript nor the names
 *       of its contributors may be used to endorse or promote products derived from this
 *       software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 */

//package org.itemscript.core.util;

/**
 * Base32 encoding/decoding class.
 * 
 * @author Robert Kaye & Gordon Mohr
 */
public final class Base32 {
  /* lookup table used to encode() groups of 5 bits of data */
  private static final String base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  /* lookup table used to decode() characters in Base32 strings */
  private static final byte[] base32Lookup = { 26, 27, 28, 29, 30, 31, -1,
      -1, -1, -1, -1, -1, -1, -1, // 23456789:;<=>?
      -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // @ABCDEFGHIJKLMNO
      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // PQRSTUVWXYZ[\]^_
      -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // `abcdefghijklmno
      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 // pqrstuvwxyz
  };
  /* Messsages for Illegal Parameter Exceptions in decode() */
  private static final String errorCanonicalLength = "non canonical Base32 string length";
  private static final String errorCanonicalEnd = "non canonical bits at end of Base32 string";
  private static final String errorInvalidChar = "invalid character in Base32 string";

  /**
   * Decode a Base32 string into an array of binary bytes. May fail if the
   * parameter is a non canonical Base32 string (the only other possible
   * exception is that the returned array cannot be allocated in memory)
   */
  static public byte[] decode(final String base32)
      throws IllegalArgumentException {
    // Note that the code below detects could detect non canonical
    // Base32 length within the loop. However canonical Base32 length
    // can be tested before entering the loop.
    // A canonical Base32 length modulo 8 cannot be:
    // 1 (aborts discarding 5 bits at STEP n=0 which produces no byte),
    // 3 (aborts discarding 7 bits at STEP n=2 which produces no byte),
    // 6 (aborts discarding 6 bits at STEP n=1 which produces no byte)
    // So these tests could be avoided within the loop.
    switch (base32.length() % 8) { // test the length of last subblock
    case 1: // 5 bits in subblock: 0 useful bits but 5 discarded
    case 3: // 15 bits in subblock: 8 useful bits but 7 discarded
    case 6: // 30 bits in subblock: 24 useful bits but 6 discarded
      throw new IllegalArgumentException(errorCanonicalLength);
    }
    byte[] bytes = new byte[base32.length() * 5 / 8];
    int offset = 0, i = 0, lookup;
    byte nextByte, digit;
    // Also the code below does test that other discarded bits
    // (1 to 4 bits at end) are effectively 0.
    while (i < base32.length()) {
      // Read the 1st char in a 8-chars subblock
      // check that chars are not outside the lookup table and valid
      lookup = base32.charAt(i++) - '2';
      if (lookup < 0 || lookup >= base32Lookup.length) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      digit = base32Lookup[lookup];
      if (digit == -1) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      // // STEP n = 0: leave 5 bits
      nextByte = (byte) (digit << 3);
      // Assert(i < base32.length) // tested before loop
      // Read the 2nd char in a 8-chars subblock
      // Check that chars are not outside the lookup table and valid
      lookup = base32.charAt(i++) - '2';
      if (lookup < 0 || lookup >= base32Lookup.length) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      digit = base32Lookup[lookup];
      if (digit == -1) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      // // STEP n = 5: insert 3 bits, leave 2 bits
      bytes[offset++] = (byte) (nextByte | (digit >> 2));
      nextByte = (byte) ((digit & 3) << 6);
      if (i >= base32.length()) {
        if (nextByte != (byte) 0) {
          throw new IllegalArgumentException(errorCanonicalEnd);
        }
        break; // discard the remaining 2 bits
      }
      // Read the 3rd char in a 8-chars subblock
      // Check that chars are not outside the lookup table and valid
      lookup = base32.charAt(i++) - '2';
      if (lookup < 0 || lookup >= base32Lookup.length) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      digit = base32Lookup[lookup];
      if (digit == -1) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      // // STEP n = 2: leave 7 bits
      nextByte |= (byte) (digit << 1);
      // Assert(i < base32.length) // tested before loop
      // Read the 4th char in a 8-chars subblock
      // Check that chars are not outside the lookup table and valid
      lookup = base32.charAt(i++) - '2';
      if (lookup < 0 || lookup >= base32Lookup.length) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      digit = base32Lookup[lookup];
      if (digit == -1) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      // // STEP n = 7: insert 1 bit, leave 4 bits
      bytes[offset++] = (byte) (nextByte | (digit >> 4));
      nextByte = (byte) ((digit & 15) << 4);
      if (i >= base32.length()) {
        if (nextByte != (byte) 0) {
          throw new IllegalArgumentException(errorCanonicalEnd);
        }
        break; // discard the remaining 4 bits
      }
      // Read the 5th char in a 8-chars subblock
      // Assert that chars are not outside the lookup table and valid
      lookup = base32.charAt(i++) - '2';
      if (lookup < 0 || lookup >= base32Lookup.length) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      digit = base32Lookup[lookup];
      if (digit == -1) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      // // STEP n = 4: insert 4 bits, leave 1 bit
      bytes[offset++] = (byte) (nextByte | (digit >> 1));
      nextByte = (byte) ((digit & 1) << 7);
      if (i >= base32.length()) {
        if (nextByte != (byte) 0) {
          throw new IllegalArgumentException(errorCanonicalEnd);
        }
        break; // discard the remaining 1 bit
      }
      // Read the 6th char in a 8-chars subblock
      // Check that chars are not outside the lookup table and valid
      lookup = base32.charAt(i++) - '2';
      if (lookup < 0 || lookup >= base32Lookup.length) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      digit = base32Lookup[lookup];
      if (digit == -1) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      // // STEP n = 1: leave 6 bits
      nextByte |= (byte) (digit << 2);
      // Assert(i < base32.length) // tested before loop
      // Read the 7th char in a 8-chars subblock
      // Check that chars are not outside the lookup table and valid
      lookup = base32.charAt(i++) - '2';
      if (lookup < 0 || lookup >= base32Lookup.length) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      digit = base32Lookup[lookup];
      if (digit == -1) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      // // STEP n = 6: insert 2 bits, leave 3 bits
      bytes[offset++] = (byte) (nextByte | (digit >> 3));
      nextByte = (byte) ((digit & 7) << 5);
      if (i >= base32.length()) {
        if (nextByte != (byte) 0) {
          throw new IllegalArgumentException(errorCanonicalEnd);
        }
        break; // discard the remaining 3 bits
      }
      // Read the 8th char in a 8-chars subblock
      // Check that chars are not outside the lookup table and valid
      lookup = base32.charAt(i++) - '2';
      if (lookup < 0 || lookup >= base32Lookup.length) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      digit = base32Lookup[lookup];
      if (digit == -1) {
        throw new IllegalArgumentException(errorInvalidChar);
      }
      // // STEP n = 3: insert 5 bits, leave 0 bit
      bytes[offset++] = (byte) (nextByte | digit);
      // // possible end of string here with no trailing bits
    }
    // On loop exit, discard trialing n bits.
    return bytes;
  }

  /**
   * Encode an array of binary bytes into a Base32 string. Should not fail
   * (the only possible exception is that the returned string cannot be
   * allocated in memory)
   */
  static public String encode(final byte[] bytes) {
    StringBuffer base32 = new StringBuffer((bytes.length * 8 + 4) / 5);
    int currByte, digit, i = 0;
    while (i < bytes.length) {
      // INVARIANTS FOR EACH STEP n in [0..5[; digit in [0..31[;
      // The remaining n bits are already aligned on top positions
      // of the 5 least bits of digit, the other bits are 0.
      // //// STEP n = 0; insert new 5 bits, leave 3 bits
      currByte = bytes[i++] & 255;
      base32.append(base32Chars.charAt(currByte >> 3));
      digit = (currByte & 7) << 2;
      if (i >= bytes.length) { // put the last 3 bits
        base32.append(base32Chars.charAt(digit));
        break;
      }
      // //// STEP n = 3: insert 2 new bits, then 5 bits, leave 1 bit
      currByte = bytes[i++] & 255;
      base32.append(base32Chars.charAt(digit | (currByte >> 6)));
      base32.append(base32Chars.charAt((currByte >> 1) & 31));
      digit = (currByte & 1) << 4;
      if (i >= bytes.length) { // put the last 1 bit
        base32.append(base32Chars.charAt(digit));
        break;
      }
      // //// STEP n = 1: insert 4 new bits, leave 4 bit
      currByte = bytes[i++] & 255;
      base32.append(base32Chars.charAt(digit | (currByte >> 4)));
      digit = (currByte & 15) << 1;
      if (i >= bytes.length) { // put the last 4 bits
        base32.append(base32Chars.charAt(digit));
        break;
      }
      // //// STEP n = 4: insert 1 new bit, then 5 bits, leave 2 bits
      currByte = bytes[i++] & 255;
      base32.append(base32Chars.charAt(digit | (currByte >> 7)));
      base32.append(base32Chars.charAt((currByte >> 2) & 31));
      digit = (currByte & 3) << 3;
      if (i >= bytes.length) { // put the last 2 bits
        base32.append(base32Chars.charAt(digit));
        break;
      }
      // /// STEP n = 2: insert 3 new bits, then 5 bits, leave 0 bit
      currByte = bytes[i++] & 255;
      base32.append(base32Chars.charAt(digit | (currByte >> 5)));
      base32.append(base32Chars.charAt(currByte & 31));
      // // This point is reached for bytes.length multiple of 5
    }
    return base32.toString();
  }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Base64 encoding/decoding.
2.Decodes Base64 data into octects
3.Implementation of MIME's Base64 encoding and decoding conversions.
4.Encode/decode for RFC 2045 Base64 as defined by RFC 2045
5.Encode/decode for RFC 2045 Base64 as defined by RFC 2045, N. Freed and N. Borenstein.
6.Encodes and decodes to and from Base64 notation.
7.Encodes hex octects into Base64
8.Helper class to provide Base64 encoding routines.
9.Represents a collection of 64 boolean (on/off) flags.
10.byte to be tested if it is Base64 alphabet
11.to Base64
12.One of the fastest implementation of the Base64 encoding. Jakarta and others are slower
13.array of byte to encode
14.Codes number up to radix 62
15.A Base64 Encoder/Decoder
16.A fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045
17.BASE64 encoder implementation
18.Base-64 Encoder - translates from base-64 text into binary
19.Base64 Character encoder as specified in RFC1113
20.Base64 Utils
21.Base64 encoder/decoder
22.Base64 from by Funambol, Inc.
23.Convert to hex from byte arrays and back
24.Converting hexadecimal strings
25.Encode and decode data in Base64 format as described in RFC 1521
26.Encode and decode integers, times, and internationalized strings to and from popular binary formats
27.Encoding of raw bytes to base64-encoded characters, and decoding of base64 characters to raw bytes
28.Performs Base64 encoding and/or decoding
29.Provides Base64 encoding and decoding as defined by RFC 2045
30.Provides Base64 encoding and decoding with URL and filename safe alphabet as defined by RFC 3548, section 4.
31.Provides utility methods to Base64 encode data
32.QP Decoder Stream
33.QP Encoder Stream
34.A class to decode Base64 streams and strings.
35.A class to encode Base64 streams and strings.
36.Encodes binary data to plain text as Base64
37.A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045.
38.Decodes InputStreams which contain binary data in base64 form
39.Base 64 Converter
40.Base64 from org.cspoker.common.util
41.Base64 converted from code at http://iharder.sourceforge.net/base64/
42.Encodes and decodes to and from Base64 notation.
43.Simple Base64 string decoding function
44.Class to represent unsigned 64-bit numbers.
45.A Base64 encoder/decoder.
46.The Base64 utility class implements Base-64 and Base-85 encoding and decoding algorithms.
47.Provides Base64 encoding and decoding
48.Code to read and write Base64-encoded text.