private void testBase64InBuffer(final int startPasSize, final int endPadSize) { final Base32 codec = new Base32(); for (final String[] element : BASE32_TEST_CASES) { final byte[] bytes = element[0].getBytes(CHARSET_UTF8); byte[] buffer = ArrayUtils.addAll(bytes, new byte[endPadSize]); buffer = ArrayUtils.addAll(new byte[startPasSize], buffer); assertEquals(element[1], StringUtils.newStringUtf8(codec.encode(buffer, startPasSize, bytes.length))); } }
@Test public void testBase32Samples() throws Exception { final Base32 codec = new Base32(); for (final String[] element : BASE32_TEST_CASES) { assertEquals(element[1], codec.encodeAsString(element[0].getBytes(CHARSET_UTF8))); } }
if (containsAlphabetOrPad(lineSeparator)) { final String sep = StringUtils.newStringUtf8(lineSeparator); throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]"); if (isInAlphabet(pad) || isWhiteSpace(pad)) { throw new IllegalArgumentException("pad must not be in alphabet or whitespace");
@Test public void testSingleCharEncoding() { for (int i = 0; i < 20; i++) { Base32 codec = new Base32(); final BaseNCodec.Context context = new BaseNCodec.Context(); final byte unencoded[] = new byte[i]; final byte allInOne[] = codec.encode(unencoded); codec = new Base32(); for (int j=0; j< unencoded.length; j++) { codec.encode(unencoded, j, 1, context); } codec.encode(unencoded, 0, -1, context); final byte singly[] = new byte[allInOne.length]; codec.readResults(singly, 0, 100, context); if (!Arrays.equals(allInOne, singly)){ fail(); } } }
/** * Creates a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the * original provided OutputStream. * * @param out * OutputStream to wrap. * @param doEncode * true if we should encode all data written to us, false if we should decode. */ public Base32OutputStream(final OutputStream out, final boolean doEncode) { super(out, new Base32(false), doEncode); }
/** * Converts a string to a byte array. * * @param base32String The input Base32 string. * @return The output byte array. */ public static byte[] getBytes(final String base32String) { final Base32 codec = new Base32(); final byte[] encodedBytes = StringEncoder.getBytes(base32String); if (!codec.isInAlphabet(encodedBytes, true)) { throw new IllegalArgumentException("malformed base32 string passed to getBytes"); } return codec.decode(encodedBytes); }
@Override public String encode(byte[] bytes) { return new Base32().encodeToString(bytes); } }
/** * Decodes a base32 String using the base32hex profile. Implementation * is case-insensitive and converts the given string to upper case before * decoding. * * @param data a base32 encoded String to decode. * @return the decoded String. */ public static byte[] decodeBase32(String data) { return data == null ? null : Base32Hex.decode(data.toUpperCase()); }
@Test public void testRandomBytes() { for (int i = 0; i < 20; i++) { final Base32 codec = new Base32(); final byte[][] b = Base32TestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } }
/** * Encodes a byte array into a base32 String using the base32hex profile. * Implementation is case-insensitive and returns encoded strings in lower case. * * @param data a byte array to encode. * @return a base32 encode String. */ public static String encodeBase32(byte[] data) { return data == null ? null : Base32Hex.encodeAsString(data).toLowerCase(); }
/** * Validates a string to ensure all its bytes are in the Base32 alphabet. * Implementation is case-insensitive and converts the given string to * upper case before evaluating. * * @param data the string to test * @return True if the given string can be decoded using Base32 */ public static boolean isBase32(String data) { return data == null ? false : Base32Hex.isInAlphabet(data.toUpperCase()); }
out = base32Encode.encode(value); } else if (method == Method.BASE64) { out = base64Encode.encode(value);
if (containsAlphabetOrPad(lineSeparator)) { final String sep = StringUtils.newStringUtf8(lineSeparator); throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]");
/** * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original * provided InputStream. * * @param in * InputStream to wrap. * @param doEncode * true if we should encode all data read from us, false if we should decode. */ public Base32InputStream(final InputStream in, final boolean doEncode) { super(in, new Base32(false), doEncode); }
/** * Converts a string to a byte array. * * @param base32String The input Base32 string. * @return The output byte array. */ public static byte[] getBytes(final String base32String) { final Base32 codec = new Base32(); final byte[] encodedBytes = StringEncoder.getBytes(base32String); if (!codec.isInAlphabet(encodedBytes, true)) { throw new IllegalArgumentException("malformed base32 string passed to getBytes"); } return codec.decode(encodedBytes); }
@Override public String encode(byte[] bytes) { return new Base32().encodeToString(bytes); } }
/** * Decodes a base32 String using the base32hex profile. Implementation * is case-insensitive and converts the given string to upper case before * decoding. * * @param data a base32 encoded String to decode. * @return the decoded String. */ public static byte[] decodeBase32(String data) { return data == null ? null : Base32Hex.decode(data.toUpperCase()); }
@Test public void testRandomBytesChunked() { for (int i = 0; i < 20; i++) { final Base32 codec = new Base32(10); final byte[][] b = Base32TestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } }
if (containsAlphabetOrPad(lineSeparator)) { final String sep = StringUtils.newStringUtf8(lineSeparator); throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]"); if (isInAlphabet(pad) || isWhiteSpace(pad)) { throw new IllegalArgumentException("pad must not be in alphabet or whitespace");