45 lines
925 B
Go
45 lines
925 B
Go
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)
|
|
}
|