forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha256truncated.go
50 lines (41 loc) · 987 Bytes
/
sha256truncated.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
// Package sha256truncated provides a sha256 hash.Hash whose output is truncated to 20 bytes (160 bits).
//
// This is the default hashing algorithm used by IAVL+ trees.
//
// s256 := sha256.New() // crypto/sha256
// s256Truncated := New() // this package
//
// // Use like any other hash.Hash ...
// // Contract:
// s256Trunc.Sum(nil) == s256.Sum(nil)[:20]
package sha256truncated
import (
"crypto/sha256"
"hash"
)
const Size = 20
// New returns a new hash.Hash computing the truncated to the first 20 bytes SHA256 checksum.
func New() hash.Hash {
return &digest{sha256.New()}
}
func (d *digest) Sum(in []byte) []byte {
return d.Hash.Sum(in)[:Size]
}
func (d *digest) Reset() {
d.Hash.Reset()
}
func (d *digest) Size() int {
return Size
}
func (d *digest) BlockSize() int {
return d.Hash.BlockSize()
}
// digest is just a wrapper around sha256
type digest struct {
hash.Hash
}
func Hash(input []byte) []byte {
h := New()
h.Write(input)
return h.Sum(nil)
}