...
1
2
3
4
5 package sha3
6
7
8
9
10
11 import (
12 "crypto"
13 "hash"
14 )
15
16
17
18
19 func New224() hash.Hash {
20 return new224()
21 }
22
23
24
25
26 func New256() hash.Hash {
27 return new256()
28 }
29
30
31
32
33 func New384() hash.Hash {
34 return new384()
35 }
36
37
38
39
40 func New512() hash.Hash {
41 return new512()
42 }
43
44 func init() {
45 crypto.RegisterHash(crypto.SHA3_224, New224)
46 crypto.RegisterHash(crypto.SHA3_256, New256)
47 crypto.RegisterHash(crypto.SHA3_384, New384)
48 crypto.RegisterHash(crypto.SHA3_512, New512)
49 }
50
51 const (
52 dsbyteSHA3 = 0b00000110
53 dsbyteKeccak = 0b00000001
54 dsbyteShake = 0b00011111
55 dsbyteCShake = 0b00000100
56
57
58
59 rateK256 = (1600 - 256) / 8
60 rateK448 = (1600 - 448) / 8
61 rateK512 = (1600 - 512) / 8
62 rateK768 = (1600 - 768) / 8
63 rateK1024 = (1600 - 1024) / 8
64 )
65
66 func new224Generic() *state {
67 return &state{rate: rateK448, outputLen: 28, dsbyte: dsbyteSHA3}
68 }
69
70 func new256Generic() *state {
71 return &state{rate: rateK512, outputLen: 32, dsbyte: dsbyteSHA3}
72 }
73
74 func new384Generic() *state {
75 return &state{rate: rateK768, outputLen: 48, dsbyte: dsbyteSHA3}
76 }
77
78 func new512Generic() *state {
79 return &state{rate: rateK1024, outputLen: 64, dsbyte: dsbyteSHA3}
80 }
81
82
83
84
85
86 func NewLegacyKeccak256() hash.Hash {
87 return &state{rate: rateK512, outputLen: 32, dsbyte: dsbyteKeccak}
88 }
89
90
91
92
93
94 func NewLegacyKeccak512() hash.Hash {
95 return &state{rate: rateK1024, outputLen: 64, dsbyte: dsbyteKeccak}
96 }
97
98
99 func Sum224(data []byte) (digest [28]byte) {
100 h := New224()
101 h.Write(data)
102 h.Sum(digest[:0])
103 return
104 }
105
106
107 func Sum256(data []byte) (digest [32]byte) {
108 h := New256()
109 h.Write(data)
110 h.Sum(digest[:0])
111 return
112 }
113
114
115 func Sum384(data []byte) (digest [48]byte) {
116 h := New384()
117 h.Write(data)
118 h.Sum(digest[:0])
119 return
120 }
121
122
123 func Sum512(data []byte) (digest [64]byte) {
124 h := New512()
125 h.Write(data)
126 h.Sum(digest[:0])
127 return
128 }
129
View as plain text