/** * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the * method treats whitespace as valid. * * @param arrayOctet * byte array to test * @return <code>true</code> if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; * <code>false</code>, otherwise * @deprecated 1.5 Use {@link #isBase64(byte[])}, will be removed in 2.0. */ @Deprecated public static boolean isArrayByteBase64(final byte[] arrayOctet) { return isBase64(arrayOctet); }
@Override public int read() throws IOException { int data = super.read(); if (!Base64.isBase64((byte) data)) { throw new IOException("Data is not base64 encoded."); } return super.read(); } }
/** * Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the * method treats whitespace as valid. * * @param base64 * String to test * @return <code>true</code> if all characters in the String are valid characters in the Base64 alphabet or if * the String is empty; <code>false</code>, otherwise * @since 1.5 */ public static boolean isBase64(final String base64) { return isBase64(StringUtils.getBytesUtf8(base64)); }
/** * Discards any characters outside of the base64 alphabet, per * the requirements on page 25 of RFC 2045 - "Any characters * outside of the base64 alphabet are to be ignored in base64 * encoded data." * * @param data The base-64 encoded data to groom * @return The data, less non-base64 characters (see RFC 2045). */ static byte[] discardNonBase64(byte[] data) { byte groomedData[] = new byte[data.length]; int bytesCopied = 0; for (int i = 0; i < data.length; i++) { if (isBase64(data[i])) { groomedData[bytesCopied++] = data[i]; } } byte packedData[] = new byte[bytesCopied]; System.arraycopy(groomedData, 0, packedData, 0, bytesCopied); return packedData; }
@Override public int read(byte[] b) throws IOException { int numRead = super.read(b); if (numRead > 0) { byte[] copy = b; if (numRead < b.length) { // isBase64 checks the whole length of byte[], we need to limit it to numRead copy = Arrays.copyOf(b, numRead); } if (!Base64.isBase64(copy)) { throw new IOException("Data is not base64 encoded."); } } return numRead; }
@Override public int read(byte[] b, int offset, int len) throws IOException { int numRead = super.read(b, offset, len); if (numRead > 0) { byte[] copy = b; if (numRead < b.length) { // isBase64 checks the whole length of byte[], we need to limit it to numRead copy = Arrays.copyOf(b, numRead); } if (!Base64.isBase64(copy)) { throw new IOException("Data is not base64 encoded."); } } return numRead; }
/** * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the * method treats whitespace as valid. * * @param arrayOctet * byte array to test * @return <code>true</code> if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; * <code>false</code>, otherwise * @since 1.5 */ public static boolean isBase64(final byte[] arrayOctet) { for (int i = 0; i < arrayOctet.length; i++) { if (!isBase64(arrayOctet[i]) && !isWhiteSpace(arrayOctet[i])) { return false; } } return true; }
public static byte[] base64decodeByteArray(String base64Str) throws Base64Exception { if(!Base64.isBase64(base64Str)) { throw new Base64Exception("Provided string is not Base64 encoded"); } byte[] out = Base64.decodeBase64(base64Str); return out; }
/** * Tests a given byte array to see if it contains * only valid characters within the Base64 alphabet. * * @param arrayOctect byte array to test * @return true if all bytes are valid characters in the Base64 * alphabet or if the byte array is empty; false, otherwise */ public static boolean isArrayByteBase64(byte[] arrayOctect) { arrayOctect = discardWhitespace(arrayOctect); int length = arrayOctect.length; if (length == 0) { // shouldn't a 0 length array be valid base64 data? // return false; return true; } for (int i = 0; i < length; i++) { if (!isBase64(arrayOctect[i])) { return false; } } return true; }
@Test public void testIsArrayByteBase64() { assertFalse(Base64.isBase64(new byte[] { Byte.MIN_VALUE })); assertFalse(Base64.isBase64(new byte[] { -125 })); assertFalse(Base64.isBase64(new byte[] { -10 })); assertFalse(Base64.isBase64(new byte[] { 0 })); assertFalse(Base64.isBase64(new byte[] { 64, Byte.MAX_VALUE })); assertFalse(Base64.isBase64(new byte[] { Byte.MAX_VALUE })); assertTrue(Base64.isBase64(new byte[] { 'A' })); assertFalse(Base64.isBase64(new byte[] { 'A', Byte.MIN_VALUE })); assertTrue(Base64.isBase64(new byte[] { 'A', 'Z', 'a' })); assertTrue(Base64.isBase64(new byte[] { '/', '=', '+' })); assertFalse(Base64.isBase64(new byte[] { '$' })); }
public static String base64decode(String base64Str) throws Base64Exception { if(!Base64.isBase64(base64Str)) { throw new Base64Exception("Provided string is not Base64 encoded"); } byte[] out = base64decodeByteArray(base64Str); CharsetDecoder decoder = Charsets.UTF_8.newDecoder(); try { decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onUnmappableCharacter(CodingErrorAction.REPORT); CharBuffer buffer = decoder.decode( ByteBuffer.wrap(Arrays.copyOf(out, out.length))); return buffer.toString(); } catch(MalformedInputException ex) { throw new Base64Exception("Input is malformed", ex); } catch(UnmappableCharacterException ex) { throw new Base64Exception("Unmappable characters found", ex); } catch(CharacterCodingException ex) { throw new Base64Exception(ex); } }
@Test public void generateRandomSecretKey() { AesCipher cipher = new AesCipher(null); String key = cipher.generateRandomSecretKey(); assertThat(StringUtils.isNotBlank(key)).isTrue(); assertThat(Base64.isBase64(key.getBytes())).isTrue(); }
@Test public void encrypt() { AesCipher cipher = new AesCipher(pathToSecretKey()); String encryptedText = cipher.encrypt("this is a secret"); assertThat(StringUtils.isNotBlank(encryptedText)).isTrue(); assertThat(Base64.isBase64(encryptedText.getBytes())).isTrue(); }
/** * Test the isStringBase64 method. */ @Test public void testIsStringBase64() { final String nullString = null; final String emptyString = ""; final String validString = "abc===defg\n\r123456\r789\r\rABC\n\nDEF==GHI\r\nJKL=============="; final String invalidString = validString + (char) 0; // append null // character try { Base64.isBase64(nullString); fail("Base64.isStringBase64() should not be null-safe."); } catch (final NullPointerException npe) { assertNotNull("Base64.isStringBase64() should not be null-safe.", npe); } assertTrue("Base64.isStringBase64(empty-string) is true", Base64.isBase64(emptyString)); assertTrue("Base64.isStringBase64(valid-string) is true", Base64.isBase64(validString)); assertFalse("Base64.isStringBase64(invalid-string) is false", Base64.isBase64(invalidString)); }
@Test public void testEncodeDecodeRandom() { for (int i = 1; i < 5; i++) { final byte[] data = new byte[this.getRandom().nextInt(10000) + 1]; this.getRandom().nextBytes(data); final byte[] enc = Base64.encodeBase64(data); assertTrue(Base64.isBase64(enc)); final byte[] data2 = Base64.decodeBase64(enc); assertTrue(Arrays.equals(data, data2)); } }
@Test public void testEncodeDecodeSmall() { for (int i = 0; i < 12; i++) { final byte[] data = new byte[i]; this.getRandom().nextBytes(data); final byte[] enc = Base64.encodeBase64(data); assertTrue("\"" + new String(enc) + "\" is Base64 data.", Base64.isBase64(enc)); final byte[] data2 = Base64.decodeBase64(enc); assertTrue(toString(data) + " equals " + toString(data2), Arrays.equals(data, data2)); } }
/** * Tests isUrlSafe. */ @Test public void testIsUrlSafe() { final Base64 base64Standard = new Base64(false); final Base64 base64URLSafe = new Base64(true); assertFalse("Base64.isUrlSafe=false", base64Standard.isUrlSafe()); assertTrue("Base64.isUrlSafe=true", base64URLSafe.isUrlSafe()); final byte[] whiteSpace = { ' ', '\n', '\r', '\t' }; assertTrue("Base64.isBase64(whiteSpace)=true", Base64.isBase64(whiteSpace)); }
public static void validate(Banner banner) throws InvalidIdentityZoneConfigurationException { if (banner != null){ if (StringUtils.hasText(banner.getLink())) { if (!UaaUrlUtils.isUrl(banner.getLink())) { throw new InvalidIdentityZoneConfigurationException("Invalid banner link: " + banner.getLink() + ". Must be a properly formatted URI beginning with http:// or https://", null); } } if (StringUtils.hasText(banner.getBackgroundColor())) { if(!hexColorPattern.matcher(banner.getBackgroundColor()).matches()) { throw new InvalidIdentityZoneConfigurationException("Invalid banner background color: " + banner.getBackgroundColor() + ". Must be a properly formatted hexadecimal color code.", null); } } if (StringUtils.hasText(banner.getTextColor())) { if(!hexColorPattern.matcher(banner.getTextColor()).matches()) { throw new InvalidIdentityZoneConfigurationException("Invalid banner text color: " + banner.getTextColor() + ". Must be a properly formatted hexadecimal color code.", null); } } if (StringUtils.hasText(banner.getLogo())) { if(!Base64.isBase64(banner.getLogo())) { throw new InvalidIdentityZoneConfigurationException("Invalid banner logo. Must be in BASE64 format.", null); } } } } }
@Test public void testNonBase64Test() throws Exception { final byte[] bArray = { '%' }; assertFalse("Invalid Base64 array was incorrectly validated as " + "an array of Base64 encoded data", Base64.isBase64(bArray)); try { final Base64 b64 = new Base64(); final byte[] result = b64.decode(bArray); assertEquals("The result should be empty as the test encoded content did " + "not contain any valid base 64 characters", 0, result.length); } catch (final Exception e) { fail("Exception was thrown when trying to decode " + "invalid base64 encoded data - RFC 2045 requires that all " + "non base64 character be discarded, an exception should not" + " have been thrown"); } }
final Long userVmId = selectResultSet.getLong(1); final String userData = selectResultSet.getString(2); if (Base64.isBase64(userData)) { final String newUserData = Base64.encodeBase64String(Base64.decodeBase64(userData.getBytes())); if (!userData.equals(newUserData)) {