package acme import ( "crypto/rand" "crypto/rsa" "fmt" "net/mail" "github.com/square/go-jose" ) // KeySize is a default RSA key size const KeySize = 2048 // Account ... type Account struct { Contact []Contact `json:"contact"` PrivKey *rsa.PrivateKey `json:"key"` signer jose.Signer nonce chan string } func newMail(email string) (Mail, error) { m, err := mail.ParseAddress(email) if err != nil { return "", err } return Mail(m.Address), nil } // NewAccount ... func NewAccount(email string, bits int) (*Account, error) { m, err := newMail(email) if err != nil { return nil, err } key, err := rsa.GenerateKey(rand.Reader, bits) if err != nil { return nil, err } return &Account{ Contact: []Contact{m}, PrivKey: key, }, nil } func LoadAccount(email string) (*Account, error) { return nil, nil } func (a *Account) Sign(msg []byte, n jose.NonceSource) ([]byte, error) { if a.signer == nil { var err error a.signer, err = jose.NewSigner(jose.RS256, a.PrivKey) if err != nil { return nil, err } a.signer.SetNonceSource(n) } 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) }