Java Code Examples for java.util.Base64#getUrlEncoder()

The following examples show how to use java.util.Base64#getUrlEncoder() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public String createToken(JSONObject payload) throws Exception {
    JSONObject header = new JSONObject();
    header.put("alg", "HS256");
    header.put("typ", "JWT");

    Encoder enc = Base64.getUrlEncoder();

    String encHeader = enc.encodeToString(header.toString().getBytes("UTF-8")).replace("=", "");
    String encPayload = enc.encodeToString(payload.toString().getBytes("UTF-8")).replace("=", "");
    String hash = calculateHash(encHeader, encPayload);

    return encHeader + "." + encPayload + "." + hash;
}
 
Example 2
Source Project: j2objc   File: Base64Test.java    License: Apache License 2.0 5 votes vote down vote up
public void testEncoder_nonPrintableBytes() throws Exception {
    Encoder encoder = Base64.getUrlEncoder();
    assertEquals("", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 0)));
    assertEquals("_w==", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 1)));
    assertEquals("_-4=", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 2)));
    assertEquals("_-7d", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 3)));
    assertEquals("_-7dzA==", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 4)));
    assertEquals("_-7dzLs=", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 5)));
    assertEquals("_-7dzLuq", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 6)));
    assertEquals("_-7dzLuqmQ==", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 7)));
    assertEquals("_-7dzLuqmYg=", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 8)));
}
 
Example 3
Source Project: j2objc   File: Base64Test.java    License: Apache License 2.0 5 votes vote down vote up
public void testEncoder_lineLength() throws Exception {
    String in_56 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd";
    String in_57 = in_56 + "e";
    String in_58 = in_56 + "ef";
    String in_59 = in_56 + "efg";
    String in_60 = in_56 + "efgh";
    String in_61 = in_56 + "efghi";

    String prefix = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5emFi";
    String out_56 = prefix + "Y2Q=";
    String out_57 = prefix + "Y2Rl";
    String out_58 = prefix + "Y2Rl\r\nZg==";
    String out_59 = prefix + "Y2Rl\r\nZmc=";
    String out_60 = prefix + "Y2Rl\r\nZmdo";
    String out_61 = prefix + "Y2Rl\r\nZmdoaQ==";

    Encoder encoder = Base64.getMimeEncoder();
    Decoder decoder = Base64.getMimeDecoder();
    assertEquals("", encodeFromAscii(encoder, decoder, ""));
    assertEquals(out_56, encodeFromAscii(encoder, decoder, in_56));
    assertEquals(out_57, encodeFromAscii(encoder, decoder, in_57));
    assertEquals(out_58, encodeFromAscii(encoder, decoder, in_58));
    assertEquals(out_59, encodeFromAscii(encoder, decoder, in_59));
    assertEquals(out_60, encodeFromAscii(encoder, decoder, in_60));
    assertEquals(out_61, encodeFromAscii(encoder, decoder, in_61));

    encoder = Base64.getUrlEncoder();
    decoder = Base64.getUrlDecoder();
    assertEquals(out_56.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_56));
    assertEquals(out_57.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_57));
    assertEquals(out_58.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_58));
    assertEquals(out_59.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_59));
    assertEquals(out_60.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_60));
    assertEquals(out_61.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_61));
}
 
Example 4
Source Project: selenium   File: Credential.java    License: Apache License 2.0 5 votes vote down vote up
public Map<String, Object> toMap() {
  Base64.Encoder encoder = Base64.getUrlEncoder();
  Map<String, Object> map = new HashMap<>();
  map.put("credentialId", encoder.encodeToString(id));
  map.put("isResidentCredential", isResidentCredential);
  map.put("rpId", rpId);
  map.put("privateKey", encoder.encodeToString(privateKey.getEncoded()));
  map.put("signCount", signCount);
  if (userHandle != null) {
    map.put("userHandle", encoder.encodeToString(userHandle));
  }
  return Collections.unmodifiableMap(map);
}
 
Example 5
Source Project: AgentX   File: FakedHttpWrapper.java    License: Apache License 2.0 4 votes vote down vote up
public FakedHttpWrapper(boolean requestMode) {
    this.requestMode = requestMode;
    this.encoder = Base64.getUrlEncoder();
    this.decoder = Base64.getUrlDecoder();
}
 
Example 6
@Override
public byte[] modifyBytes(byte[] input) {
    Base64.Encoder encoder = Base64.getUrlEncoder();
    return encoder.encode(input);
}
 
Example 7
Source Project: j2objc   File: Base64Test.java    License: Apache License 2.0 4 votes vote down vote up
public void testRoundTrip_wrap_url() throws Exception {
    Encoder encoder = Base64.getUrlEncoder();
    Decoder decoder = Base64.getUrlDecoder();
    checkRoundTrip_wrapInputStream(encoder, decoder);
}
 
Example 8
Source Project: Smack   File: Java7Base64UrlSafeEncoder.java    License: Apache License 2.0 4 votes vote down vote up
private Java7Base64UrlSafeEncoder() {
    encoder = Base64.getUrlEncoder();
    decoder = Base64.getUrlDecoder();
}