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

     1  package signature
     2  
     3  import (
     4  	"net/url"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  const (
    10  	key    = "sign"
    11  	secret = "i1ydX9RtHyuJTrw7frcu"
    12  	method = "POST"
    13  	path   = "/echo"
    14  	ttl    = time.Minute * 5
    15  )
    16  
    17  func TestSignature(t *testing.T) {
    18  	params := url.Values{}
    19  	params.Add("a", "a1")
    20  	params.Add("d", "d1")
    21  	params.Add("c", "c1 c2")
    22  	params.Add("method", method)
    23  	params.Add("path", path)
    24  
    25  	sign := New(key, secret, ttl)
    26  	authorization, date, err := sign.Generate(params)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	t.Log("authorization:", authorization)
    31  	t.Log("date:", date)
    32  	ok, err := sign.Verify(authorization, date, params)
    33  	t.Log(ok)
    34  }
    35  
    36  func TestSignatureSha256(t *testing.T) {
    37  	params := url.Values{}
    38  	params.Add("a", "a1")
    39  	params.Add("d", "d1")
    40  	params.Add("c", "c1 c2")
    41  	params.Add("method", method)
    42  	params.Add("path", path)
    43  
    44  	sign := NewSha256(key, secret, ttl)
    45  	authorization, date, err := sign.Generate(params)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	t.Log("authorization:", authorization)
    50  	t.Log("date:", date)
    51  	ok, err := sign.Verify(authorization, date, params)
    52  	t.Log(ok)
    53  }