Files
tools/internal/pwgen/pwgen.go
Michael Stapelberg e8957193c6 implement gok new
2023-01-07 16:01:39 +01:00

32 lines
533 B
Go

package pwgen
import (
"crypto/rand"
"math/big"
)
func randomChar() (byte, error) {
charset := "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"0123456789"
bn, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return 0, err
}
n := byte(bn.Int64())
return charset[n], nil
}
func RandomPassword(n int) (string, error) {
pw := make([]byte, n)
for i := 0; i < n; i++ {
c, err := randomChar()
if err != nil {
return "", err
}
pw[i] = c
}
return string(pw), nil
}