-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.go
43 lines (38 loc) · 818 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
package utils
import (
"bufio"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
)
// Filemd5Sum returns the md5 sum of a file and produces the same
// hash for both Windows and Unix systems.
func Filemd5Sum(pathToFile string) (result string, err error) {
file, err := os.Open(pathToFile)
if err != nil {
return
}
defer file.Close()
hash := md5.New()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
hash.Write(scanner.Bytes())
}
result = hex.EncodeToString(hash.Sum(nil))
return
}
func Md5Sum(s string) (result string) {
hash := md5.New()
hash.Write([]byte(s))
result = hex.EncodeToString(hash.Sum(nil))
return
}
func Hash(data string) string {
return HashBytes([]byte(data))
}
func HashBytes(data []byte) string {
sum := sha256.Sum256(data)
return fmt.Sprintf("%x", sum)
}