-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.go
223 lines (194 loc) 路 4.79 KB
/
function.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package helper
import (
"crypto/aes"
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/duxweb/go-fast/config"
"github.com/duxweb/go-fast/global"
"github.com/gofrs/uuid"
"github.com/samber/do/v2"
"github.com/spf13/afero"
"golang.org/x/crypto/bcrypt"
"math"
"math/rand"
"os"
"time"
"unicode"
)
// HashEncode Ciphertext Encryption
func HashEncode(content string) string {
hash, err := bcrypt.GenerateFromPassword([]byte(content), bcrypt.DefaultCost)
if err != nil {
return ""
}
return string(hash)
}
// HashVerify Ciphertext Verification
func HashVerify(hashedPwd string, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedPwd), []byte(password))
if err != nil {
return false
}
return true
}
func Encryption(str string, keys ...string) (string, error) {
var key string
if len(keys) == 1 {
key = keys[0]
}
if key == "" {
key = config.Load("use").GetString("app.secret")
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
ciphertext := make([]byte, len(str))
block.Encrypt(ciphertext, []byte(str))
return hex.EncodeToString(ciphertext), nil
}
func Decryption(str string, keys ...string) (string, error) {
var key string
if len(keys) == 1 {
key = keys[0]
}
if key == "" {
key = config.Load("use").GetString("app.secret")
}
ciphertext, err := hex.DecodeString(str)
if err != nil {
return "", err
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
plaintext := make([]byte, len(ciphertext))
block.Decrypt(plaintext, ciphertext)
return string(plaintext), nil
}
// PageLimit Calculation
func PageLimit(page int, total int, limit int) (int, int) {
if page <= 0 {
page = 1
}
if limit <= 0 {
return 0, 0
}
totalPage := total / limit
if total%limit != 0 {
totalPage++
}
offset := (page - 1) * limit
return offset, totalPage
}
// UcFirst Uppercase the First Letter
func UcFirst(str string) string {
for i, v := range str {
return string(unicode.ToUpper(v)) + str[i+1:]
}
return ""
}
// LcFirst Lowercase the First Letter
func LcFirst(str string) string {
for i, v := range str {
return string(unicode.ToLower(v)) + str[i+1:]
}
return ""
}
// HumanFileSize Format File Size
func HumanFileSize(fileSize int64) (size string) {
if fileSize < 1024 {
//return strconv.FormatInt(fileSize, 10) + "B"
return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
} else if fileSize < (1024 * 1024) {
return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
} else if fileSize < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
} else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
}
}
// RandString Random Characters
func RandString(len int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
bytes := make([]byte, len)
for i := 0; i < len; i++ {
b := r.Intn(26) + 65
bytes[i] = byte(b)
}
return string(bytes)
}
// Md5 Generate 32-bit MD5
func Md5(text string) string {
ctx := md5.New()
ctx.Write([]byte(text))
return hex.EncodeToString(ctx.Sum(nil))
}
// FileMd5 File MD5
func FileMd5(data []byte) string {
m := md5.New()
m.Write(data)
return hex.EncodeToString(m.Sum(nil))
}
// IsExist Determine if Directory/File Exists
func IsExist(f string) bool {
_, err := os.Stat(f)
return err == nil || os.IsExist(err)
}
func CreateDir(dirs ...string) {
fs := do.MustInvokeNamed[afero.Fs](global.Injector, "os.fs")
for _, path := range dirs {
exists, _ := afero.DirExists(fs, path)
if exists {
return
}
err := fs.MkdirAll(path, 0777)
if err != nil {
panic("failed to create " + path + " directory")
}
}
}
// GetUuid Get UUID
func GetUuid() (string, error) {
id, err := uuid.NewV4()
if err != nil {
return "", err
}
return id.String(), nil
}
// Round Keep Decimal Places
func Round(val float64, precision int) float64 {
if precision == 0 {
return math.Round(val)
}
p := math.Pow10(precision)
if precision < 0 {
return math.Floor(val*p+0.5) * math.Pow10(-precision)
}
return math.Floor(val*p+0.5) / p
}
// InTimeSpan Range Time Query
func InTimeSpan(start, end, check time.Time, includeStart, includeEnd bool) bool {
_start := start
_end := end
_check := check
if end.Before(start) {
_end = end.Add(24 * time.Hour)
if check.Before(start) {
_check = check.Add(24 * time.Hour)
}
}
if includeStart {
_start = _start.Add(-1 * time.Nanosecond)
}
if includeEnd {
_end = _end.Add(1 * time.Nanosecond)
}
return _check.After(_start) && _check.Before(_end)
}