Skip to content

Instantly share code, notes, and snippets.

@yaliv
Created March 21, 2022 01:55
Show Gist options
  • Save yaliv/4a7291f75efdb0e0e5edc07bc5da741d to your computer and use it in GitHub Desktop.
Save yaliv/4a7291f75efdb0e0e5edc07bc5da741d to your computer and use it in GitHub Desktop.
Random password generator
package password
import (
crypto_rand "crypto/rand"
"encoding/base64"
"math/big"
)
func genRandomPassword() (string, error) {
// Random length: 8-25 chars.
n, err := crypto_rand.Int(crypto_rand.Reader, big.NewInt(18))
if err != nil {
return "", err
}
length := n.Int64() + 8
// Random bytes.
pwdBytes := make([]byte, length)
_, err = crypto_rand.Read(pwdBytes)
if err != nil {
return "", err
}
pwdB64 := base64.StdEncoding.EncodeToString(pwdBytes)
return pwdB64, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment