Skip to content

Instantly share code, notes, and snippets.

@jedsada-gh
Created January 22, 2017 15:28
Show Gist options
  • Save jedsada-gh/80036703c7b32957e3fdda6b60500a5a to your computer and use it in GitHub Desktop.
Save jedsada-gh/80036703c7b32957e3fdda6b60500a5a to your computer and use it in GitHub Desktop.
try {
JsonObject header = new JsonObject();
header.addProperty("alg", "HS256");
header.addProperty("typ", "JWT");
JsonObject playload = new JsonObject();
playload.addProperty("iss", "pond");
playload.addProperty("sub", "pond");
String headerEncode = Base64.encodeToString(header.toString().getBytes(), Base64.URL_SAFE);
String playloadEncode = Base64.encodeToString(playload.toString().getBytes(), Base64.URL_SAFE);
String message = headerEncode + "." + playloadEncode;
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec("secret".getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
String hash = Base64.encodeToString(sha256_HMAC.doFinal(message.getBytes()), Base64.URL_SAFE);
String token = message + "." + hash;
System.out.println(token.replaceAll("\n", ""));
return token.replaceAll("\n", "");
} catch (Exception e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment