forked from lleiiell/goist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
72 lines (57 loc) · 1.28 KB
/
string.go
File metadata and controls
72 lines (57 loc) · 1.28 KB
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
package goist
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/google/uuid"
"strings"
"time"
"unicode"
)
// UuidSimple uuid without special characters
func UuidSimple() string {
escaper := strings.NewReplacer("-", "90", "_", "91")
return escaper.Replace(Uuid22())
}
// Uuid22 uuid of length 22
func Uuid22() string {
id := Uuid32()
bt, _ := hex.DecodeString(id)
return base64.RawURLEncoding.EncodeToString(bt)
}
// Uuid32 uuid without dash
func Uuid32() string {
return strings.Replace(uuid.New().String(), "-", "", -1)
}
// RandStr random string
// n custom length
func RandStr(n int) string {
lowerLetterRunes := []rune("0123456789abcdefghijklmnopqrstuvwxyz")
b := make([]rune, n)
for i := range b {
b[i] = lowerLetterRunes[Rand().Intn(len(lowerLetterRunes))]
time.Sleep(1 * time.Nanosecond)
}
return string(b)
}
func IsStrEmpty(str string) bool {
return strings.TrimSpace(str) == ""
}
func MD5(str string) string {
c := md5.New()
c.Write([]byte(str))
return hex.EncodeToString(c.Sum(nil))
}
// Token generate token
func Token() string {
return MD5(fmt.Sprintf("%d%d", Rand().Int63(), time.Now().UnixNano()))
}
func IsAscii(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
}