Decode a string encoded using bcrypt's base64 scheme to a byte array. Note that this is not compatible with the standard MIME-base64 encoding. @param s the string to decode @param maxolen the maximum number of bytes to decode @return an array containing the decoded bytes @throws IllegalArgumentExcep
(String s, int maxolen)
| 438 | * @throws IllegalArgumentException if maxolen is invalid |
| 439 | */ |
| 440 | static byte[] decode_base64(String s, int maxolen) |
| 441 | throws IllegalArgumentException { |
| 442 | StringBuilder rs = new StringBuilder(); |
| 443 | int off = 0, slen = s.length(), olen = 0; |
| 444 | byte ret[]; |
| 445 | byte c1, c2, c3, c4, o; |
| 446 | |
| 447 | if (maxolen <= 0) |
| 448 | throw new IllegalArgumentException ("Invalid maxolen"); |
| 449 | |
| 450 | while (off < slen - 1 && olen < maxolen) { |
| 451 | c1 = char64(s.charAt(off++)); |
| 452 | c2 = char64(s.charAt(off++)); |
| 453 | if (c1 == -1 || c2 == -1) |
| 454 | break; |
| 455 | o = (byte) (c1 << 2); |
| 456 | o |= (c2 & 0x30) >> 4; |
| 457 | rs.append((char) o); |
| 458 | if (++olen >= maxolen || off >= slen) |
| 459 | break; |
| 460 | c3 = char64(s.charAt(off++)); |
| 461 | if (c3 == -1) |
| 462 | break; |
| 463 | o = (byte) ((c2 & 0x0f) << 4); |
| 464 | o |= (c3 & 0x3c) >> 2; |
| 465 | rs.append((char) o); |
| 466 | if (++olen >= maxolen || off >= slen) |
| 467 | break; |
| 468 | c4 = char64(s.charAt(off++)); |
| 469 | o = (byte) ((c3 & 0x03) << 6); |
| 470 | o |= c4; |
| 471 | rs.append((char) o); |
| 472 | ++olen; |
| 473 | } |
| 474 | |
| 475 | ret = new byte[olen]; |
| 476 | for (off = 0; off < olen; off++) |
| 477 | ret[off] = (byte) rs.charAt(off); |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Blowfish encipher a single 64-bit block encoded as |