Skip to content

Instantly share code, notes, and snippets.

@cpu
Created August 25, 2017 14:22
Show Gist options
  • Save cpu/c1f4c3d9511c7730d512a213c2e030ac to your computer and use it in GitHub Desktop.
Save cpu/c1f4c3d9511c7730d512a213c2e030ac 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 V1 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 (
url = "http://localhost/some/acme/endpoint"
payload = `{"resource":"some-acme-endpoint"}`
)
func main() {
signerKey := jose.SigningKey{
Key: key(),
Algorithm: jose.RS256,
}
signer, err := jose.NewSigner(signerKey, &jose.SignerOptions{
NonceSource: dummyNonceSource{},
EmbedJWK: true,
})
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))
}
@unexpand
Copy link

unexpand commented Oct 5, 2017

Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment