Skip to content

Instantly share code, notes, and snippets.

@sergkondr
Created September 16, 2019 18:46
Show Gist options
  • Save sergkondr/736df7df3b25428a2256ca3a777842b6 to your computer and use it in GitHub Desktop.
Save sergkondr/736df7df3b25428a2256ca3a777842b6 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"math/rand"
"time"
)
var (
passwordLength int
useSymbols bool
letterRunes = []rune("1234567890ABCDEFGHJKLMNPQRSTUVWXYZghjkmnpqrstuvwxy")
symbolRunes = []rune("!@#$&?")
)
func init() {
flag.IntVar(&passwordLength, "length", 12, "-len 12, -l 12")
flag.IntVar(&passwordLength, "l", 12, "")
flag.BoolVar(&useSymbols, "symbols", false, "use additional symbols: [!@#$&?]")
flag.BoolVar(&useSymbols, "s", false, "")
flag.Parse()
if useSymbols {
letterRunes = append(letterRunes, symbolRunes...)
}
rand.Seed(time.Now().UnixNano())
}
func main() {
fmt.Println(getRandString(passwordLength))
}
func getRandString(l int) string {
b := make([]rune, l)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment