/
token.go
66 lines (53 loc) · 1.32 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
package cryptogy
import (
"crypto/md5"
"encoding/hex"
"strings"
"time"
"github.com/muyo/sno"
)
var (
ciper = Cipher()
saltPasswd ICipher
)
type ICipher interface {
CreatePassword(plainText string) string
VerifyPassword(plainText, cipherText string) bool
}
func Cipher() ICipher {
if saltPasswd == nil { // 8位salt值,用$符号分隔开
saltPasswd = NewSaltPassword(8, "$")
}
return saltPasswd
}
func NewSerialNo(n byte) string {
return sno.New(n).String()
}
func NewTimeSerialNo(n byte, t time.Time) string {
return sno.NewWithTime(n, t).String()
}
func Md5(data string) string {
md5Ctx := md5.New()
md5Ctx.Write([]byte(data))
cipher := md5Ctx.Sum(nil)
return hex.EncodeToString(cipher)
}
func VerifyPassword(plainText, cipherText string) bool {
return ciper.VerifyPassword(plainText, cipherText)
}
func CreatePassword(password string) string {
return ciper.CreatePassword(password)
}
func CreateMd5Password(password string) string {
password = Md5(strings.Repeat(password, 2)) // 客户端进行了哈希
return CreatePassword(password)
}
func GetPasswordChanges(password string) map[string]any {
return map[string]any{
"password": CreateMd5Password(password),
}
}
func CreateToken(prefix []byte, tailsize int) string {
tailno := RandSalt(tailsize * 2)
return hex.EncodeToString(prefix) + tailno
}