package util import ( "crypto" "crypto/rand" "crypto/rsa" "fmt" "hash" ) // Signer struct type Signer struct { opts rsa.PSSOptions method crypto.Hash h hash.Hash } // NewSigner func func NewSigner() *Signer { hash := crypto.SHA256 var opts rsa.PSSOptions opts.SaltLength = rsa.PSSSaltLengthAuto return &Signer{opts, hash, hash.New()} } func (signer *Signer) Write(p []byte) (n int, err error) { signer.h.Write(p) return } // WriteUint64 func func (signer *Signer) WriteUint64(v uint64) { signer.h.Write(Uint64ToBytes(v)) } // Sign func func (signer *Signer) Sign(key *rsa.PrivateKey) []byte { if res, err := rsa.SignPSS(rand.Reader, key, signer.method, signer.h.Sum(nil), &signer.opts); err != nil { panic(err) } else { return res } } // Verify func func (signer *Signer) Verify(key *rsa.PublicKey, sum []byte) { if err := rsa.VerifyPSS(key, signer.method, signer.h.Sum(nil), sum, &signer.opts); err != nil { panic(fmt.Errorf("Verify failed")) } } // Check func func (signer *Signer) Check(key *rsa.PublicKey, sum []byte) bool { err := rsa.VerifyPSS(key, signer.method, signer.h.Sum(nil), sum, &signer.opts) return err == nil }