public void testEncodeDecodeRandom() {
	for (int i = 1; i < 5; i++) {
		byte[] data = new byte[this.getRandom().nextInt(10000) + 1];
		this.getRandom().nextBytes(data);
		byte[] enc = Base64.encodeBase64(data);
		assertTrue(Base64.isArrayByteBase64(enc));
		byte[] data2 = Base64.decodeBase64(enc);
		assertTrue(Arrays.equals(data, data2));
	}
}

        

public void testEncodeDecodeSmall() {
	for (int i = 0; i < 12; i++) {
		byte[] data = new byte[i];
		this.getRandom().nextBytes(data);
		byte[] enc = Base64.encodeBase64(data);
		assertTrue("\"" + (new String(enc)) + "\" is Base64 data.", Base64.isArrayByteBase64(enc));
		byte[] data2 = Base64.decodeBase64(enc);
		assertTrue(toString(data) + " equals " + toString(data2), Arrays.equals(data, data2));
	}
}

        

public void testIsArrayByteBase64() {
	assertFalse(Base64.isArrayByteBase64(new byte[]{Byte.MIN_VALUE}));
	assertFalse(Base64.isArrayByteBase64(new byte[]{-125}));
	assertFalse(Base64.isArrayByteBase64(new byte[]{-10}));
	assertFalse(Base64.isArrayByteBase64(new byte[]{0}));
	assertFalse(Base64.isArrayByteBase64(new byte[]{64, Byte.MAX_VALUE}));
	assertFalse(Base64.isArrayByteBase64(new byte[]{Byte.MAX_VALUE}));
	assertTrue(Base64.isArrayByteBase64(new byte[]{'A'}));
	assertFalse(Base64.isArrayByteBase64(new byte[]{'A', Byte.MIN_VALUE}));
	assertTrue(Base64.isArrayByteBase64(new byte[]{'A', 'Z', 'a'}));
	assertTrue(Base64.isArrayByteBase64(new byte[]{'/', '=', '+'}));
	assertFalse(Base64.isArrayByteBase64(new byte[]{'$'}));
}

        

public void testIsUrlSafe() {
        Base64 base64Standard = new Base64(false);
        Base64 base64URLSafe = new Base64(true);

        assertFalse("Base64.isUrlSafe=false", base64Standard.isUrlSafe());
        assertTrue("Base64.isUrlSafe=true", base64URLSafe.isUrlSafe());

        byte[] whiteSpace = {' ', '\n', '\r', '\t'};
        assertTrue("Base64.isArrayByteBase64(whiteSpace)=true", Base64.isArrayByteBase64(whiteSpace));
}

        

String stringToBeChecked = "...";
boolean isBase64 = Base64.isArrayByteBase64(stringToBeChecked.getBytes());

        

public void generateRandomSecretKey() {
	AesCipher cipher = new AesCipher(null);
	String key = cipher.generateRandomSecretKey();
	assertThat(StringUtils.isNotBlank(key)).isTrue();
	assertThat(Base64.isArrayByteBase64(key.getBytes())).isTrue();
}

        

public static byte[] decodeIfNeeded(byte[] recv) {
	boolean arrayByteBase64 = Base64.isArrayByteBase64(recv);
	if (LOG.isDebugEnabled() && arrayByteBase64) {
		LOG.debug("Data only contains Base64 alphabets only so try to decode the data.");
	}
	return arrayByteBase64 ? Base64.decodeBase64(recv) : recv;
}

        

protected void operateOnChildNode(Content node, InstallContext ctx) throws RepositoryException, TaskExecutionException {
    if (NodeTypes.User.NAME.equals(node.getNodeTypeName())) {
      String encodedPassword = node.getNodeData("pswd").getString();

      if (StringUtils.isNotBlank(encodedPassword)) {
        byte[] pwdBytes;
        try {
          pwdBytes = encodedPassword.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
          String message = node.getName() + " password could not be hashed. User might need to reset the password before logging again.";
          log.warn(message);
          ctx.warn(message);
          pwdBytes = encodedPassword.getBytes();
        }
        if (Base64.isArrayByteBase64(pwdBytes)) {
          String pwd = new String(Base64.decodeBase64(pwdBytes));
          String hashedPwd = SecurityUtil.getBCrypt(pwd);
          node.setNodeData("pswd", hashedPwd);
        }
      }
    } else {
      // AllChildrennodeOp is not recursive!
      for (Content child : node.getChildren(filter)) {
        operateOnChildNode(child, ctx);
      }
    }
  }
}

        

public static String decodeBase64(String pEncodedString) {
  try {
    byte[] encodedBytes = pEncodedString.getBytes("UTF-8");
    // this is only a low protection, and don't say that the given bytes really base64.
    if (Base64.isArrayByteBase64(encodedBytes)) {
      return new String(Base64.decodeBase64(encodedBytes), "UTF-8");
    }
    // is not base 64 encoded
    return pEncodedString;
  } catch (UnsupportedEncodingException e) {
    throw new IllegalStateException("UTF-8 encoding unknown!", e);
  }
}

        

public void encrypt() throws Exception {
 AesCipher cipher = new AesCipher(pathToSecretKey());
 String encryptedText = cipher.encrypt("this is a secret");
 assertThat(StringUtils.isNotBlank(encryptedText)).isTrue();
 assertThat(Base64.isArrayByteBase64(encryptedText.getBytes())).isTrue();
}        
main