feat: 优化web
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package codec
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
// HashPassword 将明文密码生成为 bcrypt 哈希字符串(与业务中 bcrypt.DefaultCost 一致)。
|
||||
func HashPassword(password string) (string, error) {
|
||||
b, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
// VerifyPassword 校验明文是否与 bcrypt 哈希匹配。
|
||||
// password 明文密码
|
||||
// hashedPassword 哈希密码
|
||||
func VerifyPassword(password,hashedPassword string) error {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package id
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
generator IDGenerator
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
type IDGenerator interface {
|
||||
generate() string
|
||||
}
|
||||
|
||||
func New() string {
|
||||
once.Do(func() {
|
||||
generator = NewUUIDGenerator()
|
||||
})
|
||||
return generator.generate()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package id
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type UUIDGenerator struct {
|
||||
}
|
||||
|
||||
func NewUUIDGenerator() IDGenerator {
|
||||
return &UUIDGenerator{}
|
||||
}
|
||||
|
||||
func (g *UUIDGenerator) generate() string {
|
||||
id, _ := uuid.NewV7()
|
||||
return id.String()
|
||||
}
|
||||
Reference in New Issue
Block a user