Skip to content

Instantly share code, notes, and snippets.

@pmalek

pmalek/main.go

Created Mar 2, 2018
Embed
What would you like to do?
ss
package main
import (
"crypto/rand"
"crypto/rsa"
"encoding/json"
"fmt"
"log"
jose "gopkg.in/square/go-jose.v2"
)
func main() {
// For testing create the RSA key pair in the code
privKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Fatalf("generating random key: %v", err)
}
jwk := jose.JSONWebKey{
Key: &privKey.PublicKey,
KeyID: "myKeyID",
Algorithm: "RS256",
}
b, err := json.Marshal(jwk)
if err != nil {
panic("error marshal " + err.Error())
}
fmt.Printf("%s\n\n", b)
// create Square.jose signing key
key := jose.SigningKey{Algorithm: jose.RS256, Key: privKey}
// create a Square.jose RSA signer, used to sign the JWT
var signerOpts = jose.SignerOptions{}
signerOpts.WithType("JWT")
signerOpts.WithHeader("KeyID", "myKeyID")
rsaSigner, err := jose.NewSigner(key, &signerOpts)
if err != nil {
log.Fatalf("failed to create signer:%+v", err)
}
signature, err := rsaSigner.Sign([]byte("magic"))
if err != nil {
log.Fatal(err)
}
compactserialize, err := signature.CompactSerialize()
if err != nil {
log.Fatal(err)
}
fmt.Printf("signature %+v\n", compactserialize)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment