static byte [ ] decodeBase32 ( String str ) { // each base-32 character encodes 5 bits int numBytes = ( ( str . length ( ) * 5 ) + 7 ) / 8 ; byte [ ] result = new byte [ numBytes ] ; int resultIndex = 0 ; int which = 0 ; int working = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; int val ; if ( ch >= ' ' && ch <= ' ' ) { val = ch - ' ' ; } else if ( ch >= ' ' && ch <= ' ' ) { val = ch - ' ' ; } else if ( ch >= ' ' && ch <= ' ' ) { val = 26 + ( ch - ' ' ) ; } else if ( ch == ' ' ) { // special case which = 0 ; break ; } else { throw new IllegalArgumentException ( "Invalid base-32 character: " + ch ) ; } /* * There are probably better ways to do this but this seemed the most straightforward. */ switch ( which ) { case 0 : // all 5 bits is top 5 bits working = ( val & 0x1F ) << 3 ; which = 1 ; break ; case 1 : // top 3 bits is lower 3 bits working |= ( val & 0x1C ) >> 2 ; result [ resultIndex ++ ] = ( byte ) working ; // lower 2 bits is upper 2 bits working = ( val & 0x03 ) << 6 ; which = 2 ; break ; case 2 : // all 5 bits is mid 5 bits working |= ( val & 0x1F ) << 1 ; which = 3 ; break ; case 3 : // top 1 bit is lowest 1 bit working |= ( val & 0x10 ) >> 4 ; result [ resultIndex ++ ] = ( byte ) working ; // lower 4 bits is top 4 bits working = ( val & 0x0F ) << 4 ; which = 4 ; break ; case 4 : // top 4 bits is lowest 4 bits working |= ( val & 0x1E ) >> 1 ; result [ resultIndex ++ ] = ( byte ) working ; // lower 1 bit is top 1 bit working = ( val & 0x01 ) << 7 ; which = 5 ; break ; case 5 : // all 5 bits is mid 5 bits working |= ( val & 0x1F ) << 2 ; which = 6 ; break ; case 6 : // top 2 bits is lowest 2 bits working |= ( val & 0x18 ) >> 3 ; result [ resultIndex ++ ] = ( byte ) working ; // lower 3 bits of byte 6 is top 3 bits working = ( val & 0x07 ) << 5 ; which = 7 ; break ; case 7 : // all 5 bits is lower 5 bits working |= ( val & 0x1F ) ; result [ resultIndex ++ ] = ( byte ) working ; which = 0 ; break ; } } if ( which != 0 ) { result [ resultIndex ++ ] = ( byte ) working ; } if ( resultIndex != result . length ) { result = Arrays . copyOf ( result , resultIndex ) ; } return result ; }
Decode base - 32 method . I didn t want to add a dependency to Apache Codec just for this decode method . Exposed for testing .