generated from VLADIMIR/template
add generate and hash password
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package password_generator
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
const (
|
||||
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
)
|
||||
|
||||
type generator struct {
|
||||
length int
|
||||
}
|
||||
|
||||
func NewGenerator(length int) IPasswordGenerator {
|
||||
return &generator{
|
||||
length: length,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *generator) Generate() (string, error) {
|
||||
password := make([]byte, g.length)
|
||||
for i := range password {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
password[i] = charset[num.Int64()]
|
||||
}
|
||||
return string(password), nil
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package password_generator
|
||||
|
||||
type IPasswordGenerator interface {
|
||||
Generate() (string, error)
|
||||
}
|
||||
Reference in New Issue
Block a user