-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
67 lines (56 loc) · 1.09 KB
/
token.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
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
)
const tokenFile = ".token"
func check(e error) {
if e != nil {
panic(e)
}
}
func GetToken() string {
b, err := ioutil.ReadFile(".token")
if err != nil {
fmt.Println(".token file not found")
return ""
}
s := string(b)
return strings.Replace(s, "\n", "", -1)
}
func SaveToken() string {
token := RandStringByte(64)
err := ioutil.WriteFile(tokenFile, token, 0666)
check(err)
return string(token)
}
/**
* Check file is exist
*/
func CheckFileIsExist(filename string) bool {
var exist = true
if _, err := os.Stat(filename); os.IsNotExist(err) {
exist = false
}
return exist
}
/**
* rand token
*/
func RandStringBytesMaskImpr(n int64) string {
return string(RandStringByte(n))
}
func RandStringByte(length int64) []byte {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := int64(0); i < length; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return result
}