/
hash.go
171 lines (142 loc) · 3.29 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package cryptox
import (
"crypto/md5"
"crypto/sha256"
"crypto/sha512"
"fmt"
"io"
"os"
)
func Md5[M string | []byte](s M) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
}
func Sha256[M string | []byte](s M) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(s)))
}
func Sha224[M string | []byte](s M) string {
return fmt.Sprintf("%x", sha256.Sum224([]byte(s)))
}
func Sha512[M string | []byte](s M) string {
return fmt.Sprintf("%x", sha512.Sum512([]byte(s)))
}
func Sha384[M string | []byte](s M) string {
return fmt.Sprintf("%x", sha512.Sum384([]byte(s)))
}
func Sha512_256[M string | []byte](s M) string {
return fmt.Sprintf("%x", sha512.Sum512_256([]byte(s)))
}
func Sha512_224[M string | []byte](s M) string {
return fmt.Sprintf("%x", sha512.Sum512_224([]byte(s)))
}
func FileMd5(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
return Md5WithReader(file)
}
func FileSha256(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
return Sha256WithReader(file)
}
func FileSha224(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
return Sha224WithReader(file)
}
func FileSha512(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
return Sha512WithReader(file)
}
func FileSha384(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
return Sha384WithReader(file)
}
func FileSha512_256(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
return Sha512_256WithReader(file)
}
func FileSha512_224(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
return Sha512_224WithReader(file)
}
func Md5WithReader(file io.Reader) (string, error) {
hash := md5.New()
_, err := io.Copy(hash, file)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func Sha256WithReader(file io.Reader) (string, error) {
hash := sha256.New()
_, err := io.Copy(hash, file)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func Sha224WithReader(file io.Reader) (string, error) {
hash := sha256.New224()
_, err := io.Copy(hash, file)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func Sha512WithReader(file io.Reader) (string, error) {
hash := sha512.New()
_, err := io.Copy(hash, file)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func Sha384WithReader(file io.Reader) (string, error) {
hash := sha512.New384()
_, err := io.Copy(hash, file)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func Sha512_256WithReader(file io.Reader) (string, error) {
hash := sha512.New512_256()
_, err := io.Copy(hash, file)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func Sha512_224WithReader(file io.Reader) (string, error) {
hash := sha512.New512_224()
_, err := io.Copy(hash, file)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}