github.com/pojntfx/hydrapp/hydrapp@v0.0.0-20240516002902-d08759d6ca9f/pkg/secrets/password.go (about) 1 package secrets 2 3 import ( 4 "crypto/rand" 5 "math/big" 6 ) 7 8 const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 9 10 func GeneratePassword(length int) (string, error) { 11 password := "" 12 for i := 0; i < length; i++ { 13 num, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphabet)))) 14 if err != nil { 15 return "", err 16 } 17 18 password += string(alphabet[num.Int64()]) 19 } 20 21 return password, nil 22 }