-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.go
61 lines (51 loc) · 1.24 KB
/
password.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
package password
import (
"bytes"
"crypto/hmac"
"crypto/md5" //nolint:gosec
"crypto/sha256"
"encoding/hex"
"fmt"
"math/rand"
"time"
)
const (
saltPassword = "!@!$!dsdfdAASDFASDGASADSFAHGFKGHJKLMJLI&*)%&*%^&#$%^"
defaultPassword = "123456"
)
func GeneratePassword(str string) (password string) {
var m = md5.New() //nolint:gosec
m.Write([]byte(str))
mByte := m.Sum(nil)
// hmac
h := hmac.New(sha256.New, []byte(saltPassword))
h.Write(mByte)
password = hex.EncodeToString(h.Sum(nil))
return
}
func ResetPassword() (password string) {
var m = md5.New() //nolint:gosec
m.Write([]byte(defaultPassword))
mStr := hex.EncodeToString(m.Sum(nil))
password = GeneratePassword(mStr)
return
}
func GenerateLoginToken(id int32) (token string) {
m := md5.New() //nolint:gosec
m.Write([]byte(fmt.Sprintf("%d%s", id, saltPassword)))
token = hex.EncodeToString(m.Sum(nil))
return
}
func RandChar(size int, char string) string {
var s bytes.Buffer
// 休眠1纳秒, 避免连续两次创建的随机字符串相等
time.Sleep(time.Nanosecond)
rand.Seed(time.Now().UnixNano()) // 产生随机种子
for i := 0; i < size; i++ {
rn := rand.Int63()
l := int64(len(char))
confirm := rn % l
s.WriteByte(char[confirm])
}
return s.String()
}