-
Notifications
You must be signed in to change notification settings - Fork 16
/
hash.go
68 lines (59 loc) · 1.62 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package checksum
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
"hash/crc32"
"io"
"golang.org/x/crypto/blake2s"
)
const bufferSize = 65536
// MD5sumReader returns MD5 checksum of content in reader
func MD5sumReader(reader io.Reader) (string, error) {
return sumReader(md5.New(), reader)
}
// SHA256sumReader returns SHA256 checksum of content in reader
func SHA256sumReader(reader io.Reader) (string, error) {
return sumReader(sha256.New(), reader)
}
// SHA1sumReader returns SHA1 checksum of content in reader
func SHA1sumReader(reader io.Reader) (string, error) {
return sumReader(sha1.New(), reader)
}
// Blake2s256Reader returns SHA1 checksum of content in reader
func Blake2s256Reader(reader io.Reader) (string, error) {
hash, _ := blake2s.New256([]byte{})
return sumReader(hash, reader)
}
// CRCReader returns CRC-32-IEEE checksum of content in reader
func CRCReader(reader io.Reader) (string, error) {
table := crc32.MakeTable(crc32.IEEE)
checksum := crc32.Checksum([]byte(""), table)
buf := make([]byte, bufferSize)
for {
switch n, err := reader.Read(buf); err {
case nil:
checksum = crc32.Update(checksum, table, buf[:n])
case io.EOF:
return fmt.Sprintf("%08x", checksum), nil
default:
return "", err
}
}
}
// sumReader calculates the hash based on a provided hash provider
func sumReader(hashAlgorithm hash.Hash, reader io.Reader) (string, error) {
buf := make([]byte, bufferSize)
for {
switch n, err := reader.Read(buf); err {
case nil:
hashAlgorithm.Write(buf[:n])
case io.EOF:
return fmt.Sprintf("%x", hashAlgorithm.Sum(nil)), nil
default:
return "", err
}
}
}