12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package util
- import (
- "crypto"
- "crypto/rand"
- "crypto/rsa"
- "fmt"
- "hash"
- "code.senomas.com/go/util"
- )
- // 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(util.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"))
- }
- }
|