public void string_base64_encoding_apache() {

    String levelUpLunchURL = "http://leveluplunch.com/examples/?parm=This parameter";

    String encodedURL = Base64.encodeBase64URLSafeString(levelUpLunchURL
            .getBytes());

    assertEquals(
            "aHR0cDovL2xldmVsdXBsdW5jaC5jb20vZXhhbXBsZXMvP3Bhcm09VGhpcyBwYXJhbWV0ZXI",
            encodedURL);
}

        

public void testByteToStringVariations() throws DecoderException {
	Base64 base64 = new Base64(0);
	byte[] b1 = StringUtils.getBytesUtf8("Hello World");
	byte[] b2 = new byte[0];
	byte[] b3 = null;
	byte[] b4 = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090".toCharArray());  // for url-safe tests

	assertEquals("byteToString Hello World", "SGVsbG8gV29ybGQ=", base64.encodeToString(b1));
	assertEquals("byteToString static Hello World", "SGVsbG8gV29ybGQ=\r\n", Base64.encodeBase64String(b1));
	assertEquals("byteToString \"\"", "", base64.encodeToString(b2));
	assertEquals("byteToString static \"\"", "", Base64.encodeBase64String(b2));
	assertEquals("byteToString null", null, base64.encodeToString(b3));
	assertEquals("byteToString static null", null, Base64.encodeBase64String(b3));
	assertEquals("byteToString UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", base64.encodeToString(b4));
	assertEquals("byteToString static UUID", "K/fMJwH+Q5e0nr7tWsxwkA==\r\n", Base64.encodeBase64String(b4));
	assertEquals("byteToString static-url-safe UUID", "K_fMJwH-Q5e0nr7tWsxwkA", Base64.encodeBase64URLSafeString(b4));
}

        

private String encodeUrlSafeBase64(byte[] input) {
        return Base64.encodeBase64URLSafeString(input);
}

        

java.util.Base64.getUrlEncoder().encodeToString("my fancyurl".getBytes());
org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString("mystring".getBytes())

        

public static Message createMessageWithEmail(MimeMessage emailContent)
            throws MessagingException, IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        emailContent.writeTo(buffer);
        byte[] bytes = buffer.toByteArray();
        String encodedEmail = Base64.encodeBase64URLSafeString(bytes);
        Message message = new Message();
        message.setRaw(encodedEmail);
        return message;
}

        

public void shouldSignAndVerifyWithECDSA384() throws Exception {
    ECDSAAlgorithm algorithm384 = (ECDSAAlgorithm) Algorithm.ECDSA384((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_384, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_384, "EC"));
    String content384 = "eyJhbGciOiJFUzM4NCJ9.eyJpc3MiOiJhdXRoMCJ9";

    for (int i = 0; i < 10; i++) {
        byte[] signature = algorithm384.sign(content384.getBytes());
        String signature384 = Base64.encodeBase64URLSafeString((signature));

        String token  = content384 + "." + signature384;
        JWT jwt = JWT.require(algorithm384).withIssuer("auth0").build();
        DecodedJWT decoded = jwt.decode(token);
        algorithm384.verify(decoded, EncodeType.Base64);
    }
}

        

public void shouldDoRSA256SigningWithProvidedPrivateKey() throws Exception {
    RSAKeyProvider provider = mock(RSAKeyProvider.class);
    PrivateKey privateKey = readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA");
    PublicKey publicKey = readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA");
    when(provider.getPrivateKey()).thenReturn((RSAPrivateKey) privateKey);
    when(provider.getPublicKeyById(null)).thenReturn((RSAPublicKey) publicKey);
    Algorithm algorithm = Algorithm.RSA256(provider);
    String jwtContent = String.format("%s.%s", RS256Header, auth0IssPayload);
    byte[] contentBytes = jwtContent.getBytes(StandardCharsets.UTF_8);
    byte[] signatureBytes = algorithm.sign(contentBytes);
    String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
    String token = String.format("%s.%s", jwtContent, jwtSignature);

    assertThat(signatureBytes, is(notNullValue()));
    JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
    DecodedJWT decoded = jwt.decode(token);
    algorithm.verify(decoded, EncodeType.Base64);
}

        

private ExtensionInterfaceBean getTestBean(String instanceId,
        String subscriptionId, String organizationId) throws Exception {
    instanceAccess = Mockito.mock(InstanceAccess.class);

    serverInfo = getServerInfoMock(3);

    String encodedInstId = Base64.encodeBase64URLSafeString(
            instanceId.getBytes(StandardCharsets.UTF_8));
    String encodedSubId = Base64.encodeBase64URLSafeString(
            subscriptionId.getBytes(StandardCharsets.UTF_8));
    String encodedOrgId = Base64.encodeBase64URLSafeString(
            organizationId.getBytes(StandardCharsets.UTF_8));

    Mockito.when(instanceAccess.getAccessInfo(instanceID, subscriptionID,
            organizationID)).thenReturn("Access info from IaaS");
    Mockito.<List<? extends ServerInformation>> when(instanceAccess
            .getServerDetails(instanceID, subscriptionID, organizationID))
            .thenReturn(serverInfo);

    ExtensionInterfaceBean bean = new ExtensionInterfaceBean();
    bean.setInstanceId(encodedInstId);
    bean.setSubscriptionId(encodedSubId);
    bean.setOrganizationId(encodedOrgId);
    bean.setInstanceAccess(instanceAccess);

    return bean;
}

        

public void shouldDoECDSA512SigningWithBothKeys() throws Exception {
    Algorithm algorithm = Algorithm.ECDSA512((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_512, "EC"));
    String jwtContent = String.format("%s.%s", ES512Header, auth0IssPayload);
    byte[] contentBytes = jwtContent.getBytes(StandardCharsets.UTF_8);
    byte[] signatureBytes = algorithm.sign(contentBytes);
    String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
    String token = String.format("%s.%s", jwtContent, jwtSignature);

    assertThat(signatureBytes, is(notNullValue()));
    JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
    DecodedJWT decoded = jwt.decode(token);
    algorithm.verify(decoded, EncodeType.Base64);
}

        

public String getToken() {
	String content = String.format("%s.%s", b64(jsonMinify(getHeaderJson())), b64(jsonMinify((getPayloadJson()))));

	String signatureEncoded = Base64.encodeBase64URLSafeString(this.signature);

	return String.format("%s.%s", content, signatureEncoded);
}        
main