/
digest.go
47 lines (41 loc) · 1.05 KB
/
digest.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
package main
import (
"crypto"
"encoding/hex"
"hash"
"io"
"os"
)
// Compute an SHA256 digest for a string.
func Digest(data string) string {
hash := crypto.SHA256.New()
if _, err := hash.Write([]byte(data)); err != nil {
panic("Writing to a hash should never fail")
}
return hex.EncodeToString(hash.Sum())
}
// A reader which wraps another reader and computes a running hash code
// on the data as it is read.
type DigestReader struct {
nested io.Reader
hash hash.Hash
}
// Create a new DigestReader wrapping the specified reader.
func NewDigestReader(r io.Reader) *DigestReader {
return &DigestReader{nested: r, hash: crypto.SHA256.New()}
}
// Read data from the nested reader and update the hash data.
func (dr *DigestReader) Read(p []byte) (n int, err os.Error) {
n, err = dr.nested.Read(p)
if n > 0 {
_, werr := dr.hash.Write(p[:n])
if werr != nil {
panic("Writing to a hash should never fail")
}
}
return
}
// Get the digest for all data read so far.
func (dr *DigestReader) Digest() string {
return hex.EncodeToString(dr.hash.Sum())
}