50 lines
1005 B
Go
50 lines
1005 B
Go
package main
|
|
|
|
import (
|
|
"encoding/pem"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/lestrrat-go/jwx/v2/jwk"
|
|
)
|
|
|
|
func main() {
|
|
// Read the JWK from standard input.
|
|
jwkBytes, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
log.Fatalf("failed to read from stdin: %s", err)
|
|
}
|
|
|
|
// Parse the JWK.
|
|
key, err := jwk.ParseKey(jwkBytes)
|
|
if err != nil {
|
|
log.Fatalf("failed to parse JWK: %s", err)
|
|
}
|
|
|
|
// Get the public key.
|
|
var rawPublicKey interface{}
|
|
if err := key.Raw(&rawPublicKey); err != nil {
|
|
log.Fatalf("failed to get raw public key: %s", err)
|
|
}
|
|
|
|
// Encode the public key to PEM format. [4]
|
|
pemBytes, err := jwk.EncodePEM(key)
|
|
if err != nil {
|
|
log.Fatalf("failed to encode public key to PEM: %s", err)
|
|
}
|
|
|
|
// Write the PEM encoded public key to standard output.
|
|
block, _ := pem.Decode(pemBytes)
|
|
if block == nil {
|
|
log.Fatal("failed to decode PEM block")
|
|
}
|
|
|
|
if err := pem.Encode(os.Stdout, block); err != nil {
|
|
log.Fatalf("failed to write PEM block to stdout: %s", err)
|
|
}
|
|
|
|
fmt.Println()
|
|
}
|