jodd-core/src/main/java/jodd/util/Base32.java
public static byte[] decode(final String base32) throws IllegalArgumentException {
		switch (base32.length() % 8) {
			case 1:
			case 3:
			case 6:
				throw new IllegalArgumentException(ERR_CANONICAL_LEN);
		}

		byte[] bytes = new byte[base32.length() * 5 / 8];
		int offset = 0, i = 0, lookup;
		byte nextByte, digit;

		while (i < base32.length()) {
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// STEP n = 0: leave 5 bits
			nextByte = (byte) (digit << 3);
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// 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(ERR_CANONICAL_END);
				}
				break;
			}
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// STEP n = 2: leave 7 bits
			nextByte |= (byte) (digit << 1);
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// 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(ERR_CANONICAL_END);
				}
				break;
			}
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// 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(ERR_CANONICAL_END);
				}
				break;
			}
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// STEP n = 1: leave 6 bits
			nextByte |= (byte) (digit << 2);
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// 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(ERR_CANONICAL_END);
				}
				break;
			}
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// STEP n = 3: insert 5 bits, leave 0 bit
			bytes[offset++] = (byte) (nextByte | digit);
		}
		return bytes;
	}
public static byte[] decode(final String base32) throws IllegalArgumentException {
		switch (base32.length() % 8) {
			case 1:
			case 3:
			case 6:
				throw new IllegalArgumentException(ERR_CANONICAL_LEN);
		}

		byte[] bytes = new byte[base32.length() * 5 / 8];
		int offset = 0, i = 0, lookup;
		byte nextByte, digit;

		while (i < base32.length()) {
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// STEP n = 0: leave 5 bits
			nextByte = (byte) (digit << 3);
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// 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(ERR_CANONICAL_END);
				}
				break;
			}
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// STEP n = 2: leave 7 bits
			nextByte |= (byte) (digit << 1);
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// 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(ERR_CANONICAL_END);
				}
				break;
			}
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// 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(ERR_CANONICAL_END);
				}
				break;
			}
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// STEP n = 1: leave 6 bits
			nextByte |= (byte) (digit << 2);
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// 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(ERR_CANONICAL_END);
				}
				break;
			}
			lookup = base32.charAt(i++) - '2';
			if (lookup < 0 || lookup >= LOOKUP.length) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}
			digit = LOOKUP[lookup];
			if (digit == -1) {
				throw new IllegalArgumentException(ERR_INVALID_CHARS);
			}

			// STEP n = 3: insert 5 bits, leave 0 bit
			bytes[offset++] = (byte) (nextByte | digit);
		}
		return bytes;
	}
public static byte [ ] decode ( final String base32 ) throws IllegalArgumentException { switch ( base32 . length ( ) % 8 ) { case 1 : case 3 : case 6 : throw new IllegalArgumentException ( ERR_CANONICAL_LEN ) ; } byte [ ] bytes = new byte [ base32 . length ( ) * 5 / 8 ] ; int offset = 0 , i = 0 , lookup ; byte nextByte , digit ; while ( i < base32 . length ( ) ) { lookup = base32 . charAt ( i ++ ) - ' ' ; if ( lookup < 0 || lookup >= LOOKUP . length ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } digit = LOOKUP [ lookup ] ; if ( digit == - 1 ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } // STEP n = 0: leave 5 bits nextByte = ( byte ) ( digit << 3 ) ; lookup = base32 . charAt ( i ++ ) - ' ' ; if ( lookup < 0 || lookup >= LOOKUP . length ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } digit = LOOKUP [ lookup ] ; if ( digit == - 1 ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } // 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 ( ERR_CANONICAL_END ) ; } break ; } lookup = base32 . charAt ( i ++ ) - ' ' ; if ( lookup < 0 || lookup >= LOOKUP . length ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } digit = LOOKUP [ lookup ] ; if ( digit == - 1 ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } // STEP n = 2: leave 7 bits nextByte |= ( byte ) ( digit << 1 ) ; lookup = base32 . charAt ( i ++ ) - ' ' ; if ( lookup < 0 || lookup >= LOOKUP . length ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } digit = LOOKUP [ lookup ] ; if ( digit == - 1 ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } // 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 ( ERR_CANONICAL_END ) ; } break ; } lookup = base32 . charAt ( i ++ ) - ' ' ; if ( lookup < 0 || lookup >= LOOKUP . length ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } digit = LOOKUP [ lookup ] ; if ( digit == - 1 ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } // 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 ( ERR_CANONICAL_END ) ; } break ; } lookup = base32 . charAt ( i ++ ) - ' ' ; if ( lookup < 0 || lookup >= LOOKUP . length ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } digit = LOOKUP [ lookup ] ; if ( digit == - 1 ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } // STEP n = 1: leave 6 bits nextByte |= ( byte ) ( digit << 2 ) ; lookup = base32 . charAt ( i ++ ) - ' ' ; if ( lookup < 0 || lookup >= LOOKUP . length ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } digit = LOOKUP [ lookup ] ; if ( digit == - 1 ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } // 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 ( ERR_CANONICAL_END ) ; } break ; } lookup = base32 . charAt ( i ++ ) - ' ' ; if ( lookup < 0 || lookup >= LOOKUP . length ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } digit = LOOKUP [ lookup ] ; if ( digit == - 1 ) { throw new IllegalArgumentException ( ERR_INVALID_CHARS ) ; } // STEP n = 3: insert 5 bits, leave 0 bit bytes [ offset ++ ] = ( byte ) ( nextByte | digit ) ; } return bytes ; }
Decode a Base32 string into an array of binary bytes.
Decode a Base32 string into an array of binary bytes .