-
-
Save jedsada-gh/80036703c7b32957e3fdda6b60500a5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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