Skip to content

Instantly share code, notes, and snippets.

@stanch
Last active July 4, 2024 17:27
Show Gist options
  • Save stanch/a301d3b30456f224fa3d7e55f9d1f453 to your computer and use it in GitHub Desktop.
Save stanch/a301d3b30456f224fa3d7e55f9d1f453 to your computer and use it in GitHub Desktop.
A quick script to convert JWK public keys to .pem files
load.ivy("com.nimbusds" % "nimbus-jose-jwt" % "4.21")
load.ivy("org.bouncycastle" % "bcprov-jdk15on" % "1.51")
@
import com.nimbusds.jose.jwk._
import org.bouncycastle.util.io.pem._
import java.io._
def main(input: String) = {
val set = JWKSet.load(new File(input))
// currently hard-coded for RSA keys
val matcher = (new JWKMatcher.Builder).publicOnly(true).keyType(KeyType.RSA).keyUse(KeyUse.SIGNATURE).build
val key = (new JWKSelector(matcher)).select(set).get(0).asInstanceOf[RSAKey].toPublicKey
val output = new StringWriter
val pemWriter = new PemWriter(output)
val pem = new PemObject("RSA PUBLIC KEY", key.getEncoded)
pemWriter.writeObject(pem)
pemWriter.close()
println(output.toString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment