-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasher_test.go
64 lines (58 loc) · 1.22 KB
/
hasher_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
59
60
61
62
63
64
package main
import (
"fmt"
"testing"
)
// TestHasher validates that a given input and checksum algorithm produces the
// expected hashed value
func TestHasher(t *testing.T) {
testAlgos := []struct {
Name string
ID *ChecksumAlgorithm
Data string
Hex string
}{
{
Name: "MD5",
ID: ChecksumAlgorithmMD5,
Data: "Hello, World!",
Hex: "65a8e27d8879283831b664bd8b7f0ad4",
},
{
Name: "CRC32",
ID: ChecksumAlgorithmCRC32,
Data: "Hello, World!",
Hex: "ec4ac3d0",
},
{
Name: "CRC32C",
ID: ChecksumAlgorithmCRC32C,
Data: "Hello, World!",
Hex: "4d551068",
},
{
Name: "SHA1",
ID: ChecksumAlgorithmSHA1,
Data: "Hello, World!",
Hex: "0a0a9f2a6772942557ab5355d76af442f8f65e01",
},
{
Name: "SHA256",
ID: ChecksumAlgorithmSHA256,
Data: "Hello, World!",
Hex: "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f",
},
}
for i := 0; i < len(testAlgos); i++ {
ta := testAlgos[i]
hasher := NewHasher(ta.ID)
hash := hasher()
hash.Write([]byte(ta.Data))
sum := hash.Sum(nil)
hex := fmt.Sprintf("%x", sum)
if hex != ta.Hex {
t.Errorf("expected %s of [%s] to produce %s, got %s: %v",
ta.Name, ta.Data, ta.Hex, hex, sum)
}
}
}