-
Notifications
You must be signed in to change notification settings - Fork 73
/
util.go
77 lines (66 loc) · 1.33 KB
/
util.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
package public
import (
"crypto/md5"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"net"
"strings"
)
func GenSaltPassword(salt, password string) string {
s1 := sha256.New()
s1.Write([]byte(password))
str1 := fmt.Sprintf("%x", s1.Sum(nil))
s2 := sha256.New()
s2.Write([]byte(str1 + salt))
return fmt.Sprintf("%x", s2.Sum(nil))
}
//MD5 md5加密
func MD5(s string) string {
h := md5.New()
io.WriteString(h, s)
return fmt.Sprintf("%x", h.Sum(nil))
}
func Obj2Json(s interface{}) string {
bts, _ := json.MarshalIndent(s, "", "\t")
return string(bts)
}
func InIPSliceStr(targetIP, ipSliceStr string) bool {
if targetIP == "" || ipSliceStr == "" {
return false
}
inputSlice := strings.Split(ipSliceStr, ",")
var ipSlice []string
for _, input := range inputSlice {
ipSlice = append(ipSlice, strings.TrimSpace(input))
}
for _, ipSliceNode := range ipSlice {
ip := net.ParseIP(ipSliceNode)
if ip != nil {
// ip
if targetIP == ip.String() {
return true
}
} else {
// mask
_, mask, err := net.ParseCIDR(ipSliceNode)
if err != nil {
fmt.Println("ParseCIDR error: ", err)
continue
}
if mask.Contains(net.ParseIP(targetIP)) {
return true
}
}
}
return false
}
func InArrayString(s string, arr []string) bool {
for _, i := range arr {
if i == s {
return true
}
}
return false
}