package acme import ( "crypto/rand" "crypto/rsa" "encoding/json" "fmt" "io/ioutil" "net/mail" "github.com/square/go-jose" ) // KeySize is a default RSA key size const KeySize = 2048 // Account ... type Account struct { Contact Contacts `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 } mm := Mail(m.Address) return mm, 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: Contacts{m}, PrivKey: key, }, nil } func LoadAccount(fname string) (*Account, error) { body, err := ioutil.ReadFile(fname) if err != nil { return nil, err } a := new(Account) err = json.Unmarshal(body, a) return a, err } func (a Account) SaveAccount(fname string) error { body, err := json.MarshalIndent(a, "", "\t") if err != nil { return err } return ioutil.WriteFile(fname, body, 0600) } 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) }