Skip to content

Instantly share code, notes, and snippets.

@cpu
Created August 25, 2017 14:18
Show Gist options
  • Save cpu/4a1d2842e2ca7cda7c7b2e6dedf7ae31 to your computer and use it in GitHub Desktop.
Save cpu/4a1d2842e2ca7cda7c7b2e6dedf7ae31 to your computer and use it in GitHub Desktop.
A small Go program using go-jose.v2 to generate an example JWS in the ACME V2 style.
package main
import (
"crypto/rand"
"crypto/rsa"
"fmt"
"os"
"gopkg.in/square/go-jose.v2"
)
func die(farewell string, args ...interface{}) {
fmt.Printf(farewell, args...)
os.Exit(1)
}
func key() *rsa.PrivateKey {
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
die("Failed to generate random RSA key: %q", err.Error())
}
return key
}
// dummyNonceSource implements go-jose's NonceSource interface but returns
// a static nonce all the time.
type dummyNonceSource struct{}
func (n dummyNonceSource) Nonce() (string, error) {
return "1234", nil
}
const (
keyID = "http://localhost/reg/1234"
url = "http://localhost/some/acme/endpoint"
payload = "{}"
)
func main() {
jwk := &jose.JSONWebKey{
Key: key(),
Algorithm: "RSA",
KeyID: keyID,
}
signerKey := jose.SigningKey{
Key: jwk,
Algorithm: jose.RS256,
}
signer, err := jose.NewSigner(signerKey, &jose.SignerOptions{
NonceSource: dummyNonceSource{},
ExtraHeaders: map[jose.HeaderKey]interface{}{
"url": url,
},
})
if err != nil {
die("Failed to create NewSigner: %q", err.Error())
}
jws, err := signer.Sign([]byte(payload))
if err != nil {
die("Failed to Sign with signer: %q", err.Error())
}
output := jws.FullSerialize()
fmt.Printf("%s\n", string(output))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment