mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-14 13:24:34 +02:00
* deep-exit: bubble up error from item_metrics.go * deep-exit: bubble up error from password.go
31 lines
622 B
Go
31 lines
622 B
Go
package idgen
|
|
|
|
import (
|
|
saferand "crypto/rand"
|
|
"fmt"
|
|
"math/big"
|
|
)
|
|
|
|
const PasswordLength = 64
|
|
|
|
func GeneratePassword(length int) (string, error) {
|
|
upper := "ABCDEFGHIJKLMNOPQRSTUVWXY"
|
|
lower := "abcdefghijklmnopqrstuvwxyz"
|
|
digits := "0123456789"
|
|
|
|
charset := upper + lower + digits
|
|
charsetLength := len(charset)
|
|
|
|
buf := make([]byte, length)
|
|
|
|
for i := range length {
|
|
rInt, err := saferand.Int(saferand.Reader, big.NewInt(int64(charsetLength)))
|
|
if err != nil {
|
|
return "", fmt.Errorf("prng failed to generate unique id or password: %w", err)
|
|
}
|
|
|
|
buf[i] = charset[rInt.Int64()]
|
|
}
|
|
|
|
return string(buf), nil
|
|
}
|