一个随机密码生成器

改了一下错误,编译通过。

package main

import (
	"flag"
	"fmt"
	"math/rand"
	"time"
)

var pwdCount *int = flag.Int("n", 10, "How many password")
var pwdLength *int = flag.Int("l", 20, "How long a password")

var pwdCodes = [...]byte{
	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
	'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
	'`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
	'~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
	'[', ']', '\\', ';', '\'', ',', '.', '/',
	'{', '}', '|', ':', '"', '<', '>', '?',
}

func main() {
	flag.Parse()
	t := time.Now()
	var r *rand.Rand = rand.New(rand.NewSource(int64(t.Nanosecond())))

	for i := 0; i < *pwdCount; i++ {
		var pwd []byte = make([]byte, *pwdLength)

		for j := 0; j < *pwdLength; j++ {
			index := r.Int() % len(pwdCodes)

			pwd[j] = pwdCodes[index]
		}

		fmt.Printf("%s\n", string(pwd))
	}
}
运行结果:

(3*c{G>o3LHKy(b2I*b*
-!~{5V4mVesh*SkwLpYx
ee{**_{jk^e#p|d7Te@5
nO@Y(Ir.,9=0Xf0T_^,U
A5TIuBK)|+uq0&qc\OX%
hkSs>D.C?#}6eOmXck]j
8o$mf9yr%Wsr[>\\>cHo
nV%BBS~/^\e${kdJ1:o=
H[M)|1jKpM\K*pbfJY\%
!YK?f6oRGFMJYL]o$vYM

func (Time) Nanosecond

func (t Time) Nanosecond() int
在t确定的秒数内,Nanosecond返回此秒数中已走过的nanosecond,范围在  [0, 999999999]之间。

你可能感兴趣的:(一个随机密码生成器)