add generate and hash password

This commit is contained in:
2026-06-29 13:26:57 +07:00
parent 18fad25b27
commit 7806061313
5 changed files with 66 additions and 10 deletions
@@ -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)
}