This commit is contained in:
jiayuqi7813
2022-05-21 23:29:23 +08:00
parent b8859af6d8
commit 7cb171bc90
83 changed files with 1687 additions and 0 deletions

35
tools/token.go Normal file
View File

@@ -0,0 +1,35 @@
package tools
import (
"crypto/rand"
"fmt"
"io"
"time"
)
// Random 生成随机数。
func Random() []byte {
b := make([]byte, 32)
//ReadFull从rand.Reader精确地读取len(b)字节数据填充进b
//rand.Reader是一个全局、共享的密码用强随机数生成器
if _, err := io.ReadFull(rand.Reader, b); err != nil {
fmt.Printf("random number generation error: %v", err)
}
return b
}
// Token 生成随机token。
func Token() string {
b := Random()[:16]
return fmt.Sprintf("%x", b)
}
// Timestamp 用于获取当前10位数时间戳。
func Timestamp() int {
// time_zone := time.FixedZone("UTC", 0)
// t := time.Now().In(time_zone).Unix()
t := time.Now().Unix()
return int(t)
}