-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmac_test.go
50 lines (30 loc) · 1 KB
/
hmac_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package easyhmac
import (
"github.com/stretchr/testify/assert"
"testing"
)
var testmessage = "eyJwIjoiZEdWemRDQnRaWE56WVdkbCIsInMiOiJZbTVpYVUxUmRrNURkVEUzY0U4clFVRkVaV1Z5Wnk5bmFYZFZjV3RPTUVweVFYTlRkRWRpUVUxcVVUMD0ifQ=="
func TestEncode(t *testing.T) {
Secret = "test"
// Initialize SignedMessage struct with secret
key := SignedMessage{}
// Add payload data
key.Payload = []byte("test message")
// Create HMAC signature
key.Sign()
// Marshal message to JSON and encode in url-safe base64
signedkey, err := key.Encode()
assert.NoError(t, err, "should be no error")
assert.Equal(t, testmessage, signedkey, "they should be equal")
}
func TestDecode(t *testing.T) {
Secret = "test"
// Initialize SignedMessage struct with secret
key := SignedMessage{}
// Decode message
err := key.Decode(testmessage)
assert.NoError(t, err, "should be no error")
check := key.Verify()
assert.True(t, check, "should be true")
assert.Equal(t, []byte("test message"), key.Payload, "they should be equal")
}