-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.go
97 lines (76 loc) · 2.01 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Package hash returns the hash strings for strings and files
package hash
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"io"
"log"
"os"
)
// GetMD5FromFile returns the MD5 hash of a file.
func GetMD5FromFile(file os.File) string {
hasher := md5.New()
content, _ := os.Open(file.Name())
defer content.Close()
if _, err := io.Copy(hasher, content); err != nil {
log.Fatal(err)
}
return hex.EncodeToString(hasher.Sum(nil))
}
// GetMD5 returns the MD5 hash of a string.
func GetMD5(text string) string {
data := []byte(text)
b := md5.Sum(data)
return hex.EncodeToString(b[:])
}
// GetSHA1FromFile returns the SHA1 hash of a file.
func GetSHA1FromFile(file os.File) string {
hasher := sha1.New()
content, _ := os.Open(file.Name())
defer content.Close()
if _, err := io.Copy(hasher, content); err != nil {
log.Fatal(err)
}
return hex.EncodeToString(hasher.Sum(nil))
}
// GetSHA1 returns the SHA1 hash of a string.
func GetSHA1(text string) string {
data := []byte(text)
b := sha1.Sum(data)
return hex.EncodeToString(b[:])
}
// GetSHA256FromFile returns the SHA256 hash of a file.
func GetSHA256FromFile(file os.File) string {
hasher := sha256.New()
content, _ := os.Open(file.Name())
defer content.Close()
if _, err := io.Copy(hasher, content); err != nil {
log.Fatal(err)
}
return hex.EncodeToString(hasher.Sum(nil))
}
// GetSHA256 returns the SHA256 hash of a string.
func GetSHA256(text string) string {
data := []byte(text)
b := sha256.Sum256(data)
return hex.EncodeToString(b[:])
}
// GetSHA512FromFile returns the SHA512 hash of a file.
func GetSHA512FromFile(file os.File) string {
hasher := sha512.New()
content, _ := os.Open(file.Name())
defer content.Close()
if _, err := io.Copy(hasher, content); err != nil {
log.Fatal(err)
}
return hex.EncodeToString(hasher.Sum(nil))
}
// GetSHA512 returns the SHA512 hash of a string.
func GetSHA512(text string) string {
data := []byte(text)
b := sha512.Sum512(data)
return hex.EncodeToString(b[:])
}