Convert a base32-encoded String to binary data @param str A String containing the encoded data @return An array containing the binary data, or null if the string is invalid
(String str)
| 145 | * @return An array containing the binary data, or null if the string is invalid |
| 146 | */ |
| 147 | public byte[] |
| 148 | fromString(String str) { |
| 149 | ByteArrayOutputStream bs = new ByteArrayOutputStream(); |
| 150 | byte [] raw = str.getBytes(); |
| 151 | for (int i = 0; i < raw.length; i++) |
| 152 | { |
| 153 | char c = (char) raw[i]; |
| 154 | if (!Character.isWhitespace(c)) { |
| 155 | c = Character.toUpperCase(c); |
| 156 | bs.write((byte) c); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (padding) { |
| 161 | if (bs.size() % 8 != 0) |
| 162 | return null; |
| 163 | } else { |
| 164 | while (bs.size() % 8 != 0) |
| 165 | bs.write('='); |
| 166 | } |
| 167 | |
| 168 | byte [] in = bs.toByteArray(); |
| 169 | |
| 170 | bs.reset(); |
| 171 | DataOutputStream ds = new DataOutputStream(bs); |
| 172 | |
| 173 | for (int i = 0; i < in.length / 8; i++) { |
| 174 | short[] s = new short[8]; |
| 175 | int[] t = new int[5]; |
| 176 | |
| 177 | int padlen = 8; |
| 178 | for (int j = 0; j < 8; j++) { |
| 179 | char c = (char) in[i * 8 + j]; |
| 180 | if (c == '=') |
| 181 | break; |
| 182 | s[j] = (short) alphabet.indexOf(in[i * 8 + j]); |
| 183 | if (s[j] < 0) |
| 184 | return null; |
| 185 | padlen--; |
| 186 | } |
| 187 | int blocklen = paddingToBlockLen(padlen); |
| 188 | if (blocklen < 0) |
| 189 | return null; |
| 190 | |
| 191 | // all 5 bits of 1st, high 3 (of 5) of 2nd |
| 192 | t[0] = (s[0] << 3) | s[1] >> 2; |
| 193 | // lower 2 of 2nd, all 5 of 3rd, high 1 of 4th |
| 194 | t[1] = ((s[1] & 0x03) << 6) | (s[2] << 1) | (s[3] >> 4); |
| 195 | // lower 4 of 4th, high 4 of 5th |
| 196 | t[2] = ((s[3] & 0x0F) << 4) | ((s[4] >> 1) & 0x0F); |
| 197 | // lower 1 of 5th, all 5 of 6th, high 2 of 7th |
| 198 | t[3] = (s[4] << 7) | (s[5] << 2) | (s[6] >> 3); |
| 199 | // lower 3 of 7th, all of 8th |
| 200 | t[4] = ((s[6] & 0x07) << 5) | s[7]; |
| 201 | |
| 202 | try { |
| 203 | for (int j = 0; j < blocklen; j++) |
| 204 | ds.writeByte((byte) (t[j] & 0xFF)); |
nothing calls this directly
no test coverage detected