-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathsha256block_amd64_test.go
78 lines (63 loc) · 1.63 KB
/
sha256block_amd64_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//go:build !noasm && !appengine && gc
// +build !noasm,!appengine,gc
package sha256
import (
"crypto/sha256"
"encoding/binary"
"testing"
)
func sha256hash(m []byte) (r [32]byte) {
var h [8]uint32
h[0] = 0x6a09e667
h[1] = 0xbb67ae85
h[2] = 0x3c6ef372
h[3] = 0xa54ff53a
h[4] = 0x510e527f
h[5] = 0x9b05688c
h[6] = 0x1f83d9ab
h[7] = 0x5be0cd19
blockIntelSha(&h, m)
l0 := len(m)
l := l0 & (BlockSize - 1)
m = m[l0-l:]
var k [64]byte
copy(k[:], m)
k[l] = 0x80
if l >= 56 {
blockIntelSha(&h, k[:])
binary.LittleEndian.PutUint64(k[0:8], 0)
binary.LittleEndian.PutUint64(k[8:16], 0)
binary.LittleEndian.PutUint64(k[16:24], 0)
binary.LittleEndian.PutUint64(k[24:32], 0)
binary.LittleEndian.PutUint64(k[32:40], 0)
binary.LittleEndian.PutUint64(k[40:48], 0)
binary.LittleEndian.PutUint64(k[48:56], 0)
}
binary.BigEndian.PutUint64(k[56:64], uint64(l0)<<3)
blockIntelSha(&h, k[:])
binary.BigEndian.PutUint32(r[0:4], h[0])
binary.BigEndian.PutUint32(r[4:8], h[1])
binary.BigEndian.PutUint32(r[8:12], h[2])
binary.BigEndian.PutUint32(r[12:16], h[3])
binary.BigEndian.PutUint32(r[16:20], h[4])
binary.BigEndian.PutUint32(r[20:24], h[5])
binary.BigEndian.PutUint32(r[24:28], h[6])
binary.BigEndian.PutUint32(r[28:32], h[7])
return
}
func runTestSha(hashfunc func([]byte) [32]byte) bool {
var m = []byte("This is a message. This is a message. This is a message. This is a message.")
ar := hashfunc(m)
br := sha256.Sum256(m)
return ar == br
}
func TestSha0(t *testing.T) {
if !runTestSha(Sum256) {
t.Errorf("FAILED")
}
}
func TestSha1(t *testing.T) {
if hasIntelSha && !runTestSha(sha256hash) {
t.Errorf("FAILED")
}
}