Created
March 2, 2018 07:58
-
-
Save pmalek/80d2b0aeb82aef3ee3803d8d7faf5305 to your computer and use it in GitHub Desktop.
ss
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
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