重构
This commit is contained in:
14
core/utils/bcrypt.go
Normal file
14
core/utils/bcrypt.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package utils
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
// Encrypt 加密明文密码
|
||||
func Encrypt(password string) (string, error) {
|
||||
hashedBytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
return string(hashedBytes), err
|
||||
}
|
||||
|
||||
// Compare 密文校验
|
||||
func Compare(hashedPassword, password string) error {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
||||
}
|
||||
25
core/utils/strrand.go
Normal file
25
core/utils/strrand.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 生成编号 SeqNo 0-9A-Z
|
||||
func SeqNo(length int) string {
|
||||
items := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
|
||||
|
||||
// 创建基于时间的随机生成器
|
||||
source := rand.NewSource(time.Now().UnixNano())
|
||||
rng := rand.New(source)
|
||||
|
||||
// 生成32位长度的字符串值
|
||||
result := ""
|
||||
for i := 0; i < length; i++ {
|
||||
randomIndex := rng.Intn(len(items))
|
||||
randomItem := items[randomIndex]
|
||||
result += randomItem
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user