-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash.go
52 lines (39 loc) · 816 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
49
50
51
52
package filedriller
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
"io"
"log"
"os"
"golang.org/x/crypto/blake2b"
)
// Hashit hashes a file using the provided hash algorithm
func Hashit(inFile string, hashalg string) []byte {
fd, err := os.Open(inFile)
e(err)
defer fd.Close()
var hasher hash.Hash
if hashalg == "sha256" {
hasher = sha256.New()
} else if hashalg == "md5" {
hasher = md5.New()
} else if hashalg == "sha1" {
hasher = sha1.New()
} else if hashalg == "sha512" {
hasher = sha512.New()
} else if hashalg == "blake2b-512" {
hasher, err = blake2b.New512(nil)
e(err)
} else {
log.Println("Hash not implemented")
os.Exit(1)
}
_, err = io.Copy(hasher, fd)
io.Copy(hasher, fd)
e(err)
checksum := hasher.Sum(nil)
return checksum
}