-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha1.go
45 lines (39 loc) · 828 Bytes
/
sha1.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
package xcrypto
import (
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
)
func Sha1V1(str string) string {
h := sha1.New()
_, _ = io.WriteString(h, str)
return fmt.Sprintf("%x", h.Sum(nil))
}
func Sha1(str string) string {
hash := sha1.New()
hash.Write([]byte(str))
return hex.EncodeToString(hash.Sum(nil))
}
func Sha256V1(str string) string {
h := sha256.New()
_, _ = io.WriteString(h, str)
return fmt.Sprintf("%x", h.Sum(nil))
}
func Sha256ByByteV1(by []byte) string {
h := sha256.New()
h.Write(by)
return fmt.Sprintf("%x", h.Sum(nil))
}
// 类似php的sha1_file()
func Sha1File(path string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
hash := sha1.New()
hash.Write([]byte(data))
return hex.EncodeToString(hash.Sum(nil)), nil
}