-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_hash.go
34 lines (29 loc) · 1.15 KB
/
get_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
// -----------------------------------------------------------------------------
// github.com/balacode/udpt /[get_hash.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
package udpt
import (
"crypto/sha256"
"hash"
)
// getHash returns the SHA-256 hash of data as a slice of 32 bytes.
func getHash(data []byte) []byte {
return getHashDI(data, sha256.New())
} // getHash
// getHashDI is only used by getHash() and provides parameters
// for dependency injection, to enable mocking during testing.
func getHashDI(data []byte, hs hash.Hash) []byte {
n, err := hs.Write(data)
if n != len(data) || err != nil {
// this should never happen (see hash.Hash.Write in Go docs)
panic(makeError(0xE51EC0, err).Error())
}
ret := hs.Sum(nil)
if len(ret) != 32 {
// this should never happen
panic(makeError(0xE4D3E1, "invalid hash size").Error())
}
return ret
} // getHashDI
// end