2 * Copyright 2009 Google Inc. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.android.apps.authenticator;
19 import java.util.HashMap;
20 import java.util.Locale;
23 * Encodes arbitrary byte arrays as case-insensitive base-32 strings.
25 * The implementation is slightly different than in RFC 4648. During encoding,
26 * padding is not added, and during decoding the last incomplete chunk is not
27 * taken into account. The result is that multiple strings decode to the same
28 * byte array, for example, string of sixteen 7s ("7...7") and seventeen 7s both
29 * decode to the same byte array.
30 * TODO(sarvar): Revisit this encoding and whether this ambiguity needs fixing.
32 * @author sweis@google.com (Steve Weis)
35 public class Base32String {
38 private static final Base32String INSTANCE =
39 new Base32String("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"); // RFC 4648/3548
41 static Base32String getInstance() {
45 // 32 alpha-numeric characters.
46 private String ALPHABET;
47 private char[] DIGITS;
50 private HashMap<Character, Integer> CHAR_MAP;
52 static final String SEPARATOR = "-";
54 protected Base32String(String alphabet) {
55 this.ALPHABET = alphabet;
56 DIGITS = ALPHABET.toCharArray();
57 MASK = DIGITS.length - 1;
58 SHIFT = Integer.numberOfTrailingZeros(DIGITS.length);
59 CHAR_MAP = new HashMap<Character, Integer>();
60 for (int i = 0; i < DIGITS.length; i++) {
61 CHAR_MAP.put(DIGITS[i], i);
65 public static byte[] decode(String encoded) throws DecodingException {
66 return getInstance().decodeInternal(encoded);
69 protected byte[] decodeInternal(String encoded) throws DecodingException {
70 // Remove whitespace and separators
71 encoded = encoded.trim().replaceAll(SEPARATOR, "").replaceAll(" ", "");
73 // Remove padding. Note: the padding is used as hint to determine how many
74 // bits to decode from the last incomplete chunk (which is commented out
75 // below, so this may have been wrong to start with).
76 encoded = encoded.replaceFirst("[=]*$", "");
78 // Canonicalize to all upper case
79 encoded = encoded.toUpperCase(Locale.US);
80 if (encoded.length() == 0) {
83 int encodedLength = encoded.length();
84 int outLength = encodedLength * SHIFT / 8;
85 byte[] result = new byte[outLength];
89 for (char c : encoded.toCharArray()) {
90 if (!CHAR_MAP.containsKey(c)) {
91 throw new DecodingException("Illegal character: " + c);
94 buffer |= CHAR_MAP.get(c) & MASK;
97 result[next++] = (byte) (buffer >> (bitsLeft - 8));
101 // We'll ignore leftover bits for now.
103 // if (next != outLength || bitsLeft >= SHIFT) {
104 // throw new DecodingException("Bits left: " + bitsLeft);
109 public static String encode(byte[] data) {
110 return getInstance().encodeInternal(data);
113 protected String encodeInternal(byte[] data) {
114 if (data.length == 0) {
118 // SHIFT is the number of bits per output character, so the length of the
119 // output is the length of the input multiplied by 8/SHIFT, rounded up.
120 if (data.length >= (1 << 28)) {
121 // The computation below will fail, so don't do it.
122 throw new IllegalArgumentException();
125 int outputLength = (data.length * 8 + SHIFT - 1) / SHIFT;
126 StringBuilder result = new StringBuilder(outputLength);
128 int buffer = data[0];
131 while (bitsLeft > 0 || next < data.length) {
132 if (bitsLeft < SHIFT) {
133 if (next < data.length) {
135 buffer |= (data[next++] & 0xff);
138 int pad = SHIFT - bitsLeft;
143 int index = MASK & (buffer >> (bitsLeft - SHIFT));
145 result.append(DIGITS[index]);
147 return result.toString();
151 // enforce that this class is a singleton
152 public Object clone() throws CloneNotSupportedException {
153 throw new CloneNotSupportedException();
156 public static class DecodingException extends Exception {
157 public DecodingException(String message) {