forked from wenlng/go-captcha
/
bcrypt.go
64 lines (59 loc) 路 1.02 KB
/
bcrypt.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
/**
* @Author Awen
* @Description
* @Date 2021/7/16
**/
package captcha
import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"sync/atomic"
"time"
)
// Md5ToString is a function
/**
* @Description: Md5
* @param str
* @return string
*/
func Md5ToString(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
// GenUniqueId is a function
/**
* @Description: GenUniqueId
* @param str
* @return string
*/
var num int64
func GenUniqueId() string {
t := time.Now()
s := t.Format("20060102150405")
m := t.UnixNano() / 1e6 - t.UnixNano() / 1e9 * 1e3
ms := Sup(m, 3)
p := os.Getpid() % 1000
ps := Sup(int64(p), 3)
i := atomic.AddInt64(&num, 1)
r := i % 10000
rs := Sup(r, 4)
n := fmt.Sprintf("%s%s%s%s", s, ms, ps, rs)
return n
}
// Sup is a function
/**
* @Description: 瀵归暱搴︿笉瓒硁鐨勬暟瀛楀墠闈㈣ˉ0
* @param int64
* @param int
* @return string
*/
func Sup(i int64, n int) string {
m := fmt.Sprintf("%d", i)
for len(m) < n {
m = fmt.Sprintf("0%s", m)
}
return m
}