/
checksum.go
91 lines (75 loc) · 1.64 KB
/
checksum.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package checksum
import (
"crypto"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
"hash"
"io"
"math"
"os"
)
const fileChunk = 8192 //8K
func createHash(method crypto.Hash) (hash.Hash, error) {
var h hash.Hash
switch method {
case crypto.MD5:
h = md5.New()
case crypto.SHA1:
h = sha1.New()
case crypto.SHA224:
h = sha256.New224()
case crypto.SHA256:
h = sha256.New()
case crypto.SHA384:
h = sha512.New384()
case crypto.SHA512:
h = sha512.New()
default:
return h, errors.New("Unsupported hashing method")
}
return h, nil
}
//String returns a hash for the given string
func String(s string, method crypto.Hash) (string, error) {
h, err := createHash(method)
if err != nil {
return "", err
}
io.WriteString(h, s)
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
//Bytes return a hash for the given bytes
func Bytes(b []byte, method crypto.Hash) (string, error) {
h, err := createHash(method)
if err != nil {
return "", err
}
io.WriteString(h, string(b))
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
//File returns a hash for the given file
func File(path string, method crypto.Hash) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
stat, _ := file.Stat()
size := stat.Size()
chunks := uint64(math.Ceil(float64(size) / float64(fileChunk)))
h, err := createHash(method)
if err != nil {
return "", err
}
for i := uint64(0); i < chunks; i++ {
csize := int(math.Min(fileChunk, float64(size-int64(i*fileChunk))))
buf := make([]byte, csize)
file.Read(buf)
io.WriteString(h, string(buf))
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}