github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/signature/verify.go (about)

     1  package signature
     2  
     3  import (
     4  	"encoding/base64"
     5  	"errors"
     6  	"fmt"
     7  	"time"
     8  
     9  	"github.com/binbinly/pkg/util"
    10  )
    11  
    12  // Verify verifies the signature
    13  func (s *signature) Verify(auth string, timestamp int64, params any) (bool, error) {
    14  	if timestamp == 0 {
    15  		return false, errors.New("date required")
    16  	}
    17  	ts := time.Unix(timestamp, 0)
    18  	if util.SubInLocation(ts) > float64(s.ttl/time.Second) {
    19  		return false, errors.New("date exceeds limit")
    20  	}
    21  
    22  	buffer, err := s.data(timestamp, params)
    23  	if err != nil {
    24  		return false, err
    25  	}
    26  
    27  	digest := base64.StdEncoding.EncodeToString(s.cryptoFunc(buffer, []byte(s.secret)))
    28  
    29  	return auth == fmt.Sprintf("%s %s", s.key, digest), nil
    30  }