Created
April 28, 2024 07:09
-
-
Save 1buran/51aff4c6f22d12dda44f544439138071 to your computer and use it in GitHub Desktop.
Generator of passwords
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"os" | |
"strconv" | |
"strings" | |
) | |
var template []string | |
const DEFAULT_LEN = 16 | |
func init() { | |
// printable ASCII symbols range is [32, 127), | |
// the start of a range is a space symbol (32), | |
// for password generation it may confuse when appears at the end of string, | |
// so let's exclude it from a range. | |
for i := 33; i < 127; i++ { | |
template = append(template, string(rune(i))) | |
} | |
} | |
func genRandomString(n int) string { | |
var b strings.Builder | |
for i := 0; i < n; i++ { | |
fmt.Fprint(&b, template[rand.Intn(len(template))]) | |
} | |
return b.String() | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println(genRandomString(DEFAULT_LEN)) | |
} else { | |
n, err := strconv.Atoi(os.Args[1]) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
fmt.Println(genRandomString(n)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment