forked from go-pay/gopay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
133 lines (118 loc) · 3.27 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
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
package gopay
import (
"math/rand"
"strconv"
"time"
)
// 获取随机字符串
// length:字符串长度
func GetRandomString(length int) string {
str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
var (
result []byte
b []byte
r *rand.Rand
)
b = []byte(str)
r = rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < length; i++ {
result = append(result, b[r.Intn(len(b))])
}
return string(result)
}
// 解析时间
func ParseDateTime(timeStr string) (datetime time.Time) {
datetime, _ = time.ParseInLocation(TimeLayout, timeStr, time.Local)
return
}
// 字符串转Float64
func String2Float64(floatStr string) (floatNum float64) {
floatNum, _ = strconv.ParseFloat(floatStr, 64)
return
}
// 字符串转Float32
func String2Float32(floatStr string) (floatNum float32) {
float, _ := strconv.ParseFloat(floatStr, 32)
floatNum = float32(float)
return
}
// Float64转字符串
// floatNum:float64数字
// prec:精度位数(不传则默认float数字精度)
func Float64ToString(floatNum float64, prec ...int) (floatStr string) {
if len(prec) > 0 {
floatStr = strconv.FormatFloat(floatNum, 'f', prec[0], 64)
return
}
floatStr = strconv.FormatFloat(floatNum, 'f', -1, 64)
return
}
// Float32转字符串
// floatNum:float32数字
// prec:精度位数(不传则默认float数字精度)
func Float32ToString(floatNum float32, prec ...int) (floatStr string) {
if len(prec) > 0 {
floatStr = strconv.FormatFloat(float64(floatNum), 'f', prec[0], 32)
return
}
floatStr = strconv.FormatFloat(float64(floatNum), 'f', -1, 32)
return
}
// 字符串转Int
func String2Int(intStr string) (intNum int) {
intNum, _ = strconv.Atoi(intStr)
return
}
// 字符串转Int32
func String2Int32(intStr string) (int32Num int32) {
intNum, _ := strconv.Atoi(intStr)
int32Num = int32(intNum)
return
}
// 字符串转Int64
func String2Int64(intStr string) (int64Num int64) {
intNum, _ := strconv.Atoi(intStr)
int64Num = int64(intNum)
return
}
// Int转字符串
func Int2String(intNum int) (intStr string) {
intStr = strconv.Itoa(intNum)
return
}
// Int32转字符串
func Int322String(intNum int32) (int32Str string) {
// 10, 代表10进制
int32Str = strconv.FormatInt(int64(intNum), 10)
return
}
// Int64转字符串
func Int642String(intNum int64) (int64Str string) {
// 10, 代表10进制
int64Str = strconv.FormatInt(intNum, 10)
return
}
// 解密填充模式(去除补全码) PKCS7UnPadding
// 解密时,需要在最后面去掉加密时添加的填充byte
func PKCS7UnPadding(origData []byte) (bs []byte) {
length := len(origData)
unPaddingNumber := int(origData[length-1]) // 找到Byte数组最后的填充byte 数字
if unPaddingNumber <= 16 {
bs = origData[:(length - unPaddingNumber)] // 只截取返回有效数字内的byte数组
} else {
bs = origData
}
return
}
// 解密填充模式(去除补全码) PKCS5UnPadding
// 解密时,需要在最后面去掉加密时添加的填充byte
func PKCS5UnPadding(origData []byte) (bs []byte) {
length := len(origData)
unPaddingNumber := int(origData[length-1]) // 找到Byte数组最后的填充byte
if unPaddingNumber <= 16 {
bs = origData[:(length - unPaddingNumber)] // 只截取返回有效数字内的byte数组
} else {
bs = origData
}
return
}