增加备份和导出功能
This commit is contained in:
44
server/utils/password.go
Normal file
44
server/utils/password.go
Normal file
@ -0,0 +1,44 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type Bcrypt struct {
|
||||
cost int
|
||||
}
|
||||
|
||||
func (b *Bcrypt) Encode(password []byte) ([]byte, error) {
|
||||
return bcrypt.GenerateFromPassword(password, b.cost)
|
||||
}
|
||||
|
||||
func (b *Bcrypt) Match(hashedPassword, password []byte) error {
|
||||
return bcrypt.CompareHashAndPassword(hashedPassword, password)
|
||||
}
|
||||
|
||||
var Encoder = Bcrypt{
|
||||
cost: bcrypt.DefaultCost,
|
||||
}
|
||||
|
||||
func GenPassword() string {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
digits := "0123456789"
|
||||
specials := "~=+%^*/()[]{}/!@#$?|"
|
||||
all := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
||||
"abcdefghijklmnopqrstuvwxyz" +
|
||||
digits + specials
|
||||
length := 8
|
||||
buf := make([]byte, length)
|
||||
buf[0] = digits[rand.Intn(len(digits))]
|
||||
buf[1] = specials[rand.Intn(len(specials))]
|
||||
for i := 2; i < length; i++ {
|
||||
buf[i] = all[rand.Intn(len(all))]
|
||||
}
|
||||
rand.Shuffle(len(buf), func(i, j int) {
|
||||
buf[i], buf[j] = buf[j], buf[i]
|
||||
})
|
||||
return string(buf)
|
||||
}
|
Reference in New Issue
Block a user