Created
August 25, 2017 14:22
-
-
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.
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" | |
"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)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much.