-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
41 lines (39 loc) · 942 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
package common
import (
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"io"
"os"
)
// HashFile generates a string hash of the given file path. Supported hashing algorithm: sha256, sha384, and sha512.
func HashFile(algorithm string, filepath string) (value string, err error) {
f, err := os.Open(filepath) // #nosec
if err != nil {
err = fmt.Errorf("couldn't open %s: %w", filepath, err)
return
}
var hasher hash.Hash
switch algorithm {
case "sha256", "256":
hasher = sha256.New()
case "sha384", "384":
hasher = sha512.New384()
case "sha512", "512":
hasher = sha512.New()
default:
err = fmt.Errorf("unsupported algorithm %s ", algorithm)
return
}
if _, err = io.Copy(hasher, f); err != nil {
err = fmt.Errorf("couldn't hash %s: %w", filepath, err)
return
}
if err = f.Close(); err != nil {
err = fmt.Errorf("error closing file: %w", err)
return
}
value = fmt.Sprintf("%x", hasher.Sum(nil))
return
}