-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthhash_test.go
58 lines (52 loc) · 1.5 KB
/
authhash_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
51
52
53
54
55
56
57
58
package smartid
import (
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"reflect"
"testing"
)
var randHashTest = []byte{65, 65, 65, 65, 65, 65, 65, 65, 65, 65}
func TestGenerateAuthHash(t *testing.T) {
randByteGenerator = func(n int) []byte {
return randHashTest
}
var hash AuthHash
hash = GenerateAuthHash(SHA256)
if len(hash) != sha256.Size {
t.Error("expected", sha256.Size, "got", len(hash))
}
hash = GenerateAuthHash(SHA384)
if len(hash) != sha512.Size384 {
t.Error("expected", sha512.Size384, "got", len(hash))
}
hash = GenerateAuthHash(SHA512)
if len(hash) != sha512.Size {
t.Error("expected", sha512.Size, "got", len(hash))
}
}
func TestHashSum_EncodeBase64(t *testing.T) {
hash := GenerateAuthHash(SHA512)
hashEncoded := hash.EncodeBase64()
result := make([]byte, base64.StdEncoding.EncodedLen(len(hash)))
base64.StdEncoding.Encode(result, hash)
if !reflect.DeepEqual(hashEncoded, result) {
t.Error("expected", result, "got", hashEncoded)
}
}
func TestHashSum_ToBase64String(t *testing.T) {
hash := GenerateAuthHash(SHA512)
hashEncoded := hash.ToBase64String()
result := make([]byte, base64.StdEncoding.EncodedLen(len(hash)))
base64.StdEncoding.Encode(result, hash)
if string(hashEncoded) != string(result) {
t.Error("expected", string(result), "got", string(hashEncoded))
}
}
func TestAuthHash_CalculateVerificationCode(t *testing.T) {
vc := GenerateAuthHash(SHA512).CalculateVerificationCode()
result := "3174"
if vc != result {
t.Error("expected", result, "got", vc)
}
}