3737 secp256k1_halfN = new (big.Int ).Div (secp256k1_N , big .NewInt (2 ))
3838)
3939
40+ // Hasher is a repetitive hasher allowing the same hash data structures to be
41+ // reused between hash runs instead of requiring new ones to be created.
42+ type Hasher func (data []byte ) []byte
43+
44+ // Keccak256 calculates and returns the Keccak256 hash of the input data.
4045func Keccak256 (data ... []byte ) []byte {
4146 d := sha3 .NewKeccak256 ()
4247 for _ , b := range data {
@@ -45,6 +50,8 @@ func Keccak256(data ...[]byte) []byte {
4550 return d .Sum (nil )
4651}
4752
53+ // Keccak256Hash calculates and returns the Keccak256 hash of the input data,
54+ // converting it to an internal Hash data structure.
4855func Keccak256Hash (data ... []byte ) (h common.Hash ) {
4956 d := sha3 .NewKeccak256 ()
5057 for _ , b := range data {
@@ -54,6 +61,47 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) {
5461 return h
5562}
5663
64+ // Keccak256Hasher creates a repetitive Keccak256 hasher, allowing the same hash
65+ // data structures to be reused between hash runs instead of requiring new ones
66+ // to be created.
67+ //
68+ // The returned function is not thread safe!
69+ func Keccak256Hasher () Hasher {
70+ hasher := sha3 .NewKeccak256 ()
71+
72+ return func (data []byte ) []byte {
73+ hasher .Write (data )
74+ result := hasher .Sum (nil )
75+ hasher .Reset ()
76+ return result
77+ }
78+ }
79+
80+ // Keccak512 calculates and returns the Keccak512 hash of the input data.
81+ func Keccak512 (data ... []byte ) []byte {
82+ d := sha3 .NewKeccak512 ()
83+ for _ , b := range data {
84+ d .Write (b )
85+ }
86+ return d .Sum (nil )
87+ }
88+
89+ // Keccak512Hasher creates a repetitive Keccak512 hasher, allowing the same hash
90+ // data structures to be reused between hash runs instead of requiring new ones
91+ // to be created.
92+ //
93+ // The returned function is not thread safe!
94+ func Keccak512Hasher () Hasher {
95+ hasher := sha3 .NewKeccak512 ()
96+
97+ return func (data []byte ) []byte {
98+ hasher .Write (data )
99+ result := hasher .Sum (nil )
100+ hasher .Reset ()
101+ return result
102+ }
103+ }
104+
57105// Deprecated: For backward compatibility as other packages depend on these
58106func Sha3Hash (data ... []byte ) common.Hash { return Keccak256Hash (data ... ) }
59107
0 commit comments