-
Notifications
You must be signed in to change notification settings - Fork 568
/
hash.go
48 lines (40 loc) · 990 Bytes
/
hash.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
package pachhash
import (
"encoding/hex"
"hash"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"golang.org/x/crypto/blake2b"
)
// OutputSize is the size of an Output in bytes
const OutputSize = 32
// Output is the output of the hash function.
// Sum returns an Output
type Output = [OutputSize]byte
// New creates a new hasher.
func New() hash.Hash {
h, err := blake2b.New256(nil)
if err != nil {
panic(err)
}
return h
}
// Sum computes a hash sum for a set of bytes.
func Sum(data []byte) Output {
return blake2b.Sum256(data)
}
// ParseHex parses a hex string into output.
func ParseHex(x []byte) (*Output, error) {
o := Output{}
n, err := hex.Decode(o[:], x)
if err != nil {
return nil, errors.EnsureStack(err)
}
if n < OutputSize {
return nil, errors.Errorf("hex string too short to be Output")
}
return &o, nil
}
// EncodeHash encodes a hash into a string representation.
func EncodeHash(bytes []byte) string {
return hex.EncodeToString(bytes)
}