Skip to content

Instantly share code, notes, and snippets.

@FrankDeGroot
Created February 1, 2022 11:28
Show Gist options
  • Save FrankDeGroot/3b01338eaebaca9abcaced6748560801 to your computer and use it in GitHub Desktop.
Save FrankDeGroot/3b01338eaebaca9abcaced6748560801 to your computer and use it in GitHub Desktop.
Create JWS from PEM
private String readPublicKey() throws Exception {
val key = new String(this.getClass().getResourceAsStream("/publicKey.pem").readAllBytes())
.replace("-----BEGIN PUBLIC KEY-----", "")
.replaceAll(System.lineSeparator(), "")
.replace("-----END PUBLIC KEY-----", "");
val encoded = Base64.getDecoder().decode(key);
val keySpec = new X509EncodedKeySpec(encoded);
val keyFactory = KeyFactory.getInstance("RSA");
val jsonWebKey = PublicJsonWebKey.Factory.newPublicJwk(keyFactory.generatePublic(keySpec));
val jsonWebKeySet = new JsonWebKeySet(jsonWebKey);
return jsonWebKeySet.toJson();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment