package acme import ( "crypto/rand" "crypto/rsa" "fmt" "net/mail" "github.com/square/go-jose" ) type Account struct { Contact []string `json:"contact"` PrivKey *rsa.PrivateKey `json:"key"` Signer jose.Signer `json:"-"` } func NewAccount(email string, bits int) (Account, error) { m, err := mail.ParseAddress(email) if err != nil { return Account{}, err } key, err := rsa.GenerateKey(rand.Reader, bits) if err != nil { return Account{}, err } return Account{ Contact: []string{"mailto:" + m.Address}, PrivKey: key, }, nil } func (a *Account) Sign(msg []byte) ([]byte, error) { if a.Signer == nil { signer, err := jose.NewSigner(jose.RS256, a.PrivKey) if err != nil { return nil, err } signer.SetNonceSource(nonces) a.Signer = signer } obj, err := a.Signer.Sign(msg) return []byte(obj.FullSerialize()), err } func (a *Account) ParseSigned(msg []byte) ([]byte, error) { fmt.Println("MSG", string(msg)) obj, err := jose.ParseSigned(string(msg)) if err != nil { return nil, err } return obj.Verify(&a.PrivKey.PublicKey) }