囫囵吞枣一番,折腾了一个Go语言生成密码串的
package main
import (
"flag"
"fmt"
"math/rand"
"time"
)
var(
salt string
length int
charset string
)
const (
NUmStr = "0123456789"
CharStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
SpecStr = "+=-@#~,.[]()!%^*$"
)
func parseArgs(){
flag.IntVar(&length,"l",6,"-l 密码长度")
flag.StringVar(&salt,"s","","-s 加盐")
flag.StringVar(&charset, "t", "num",
//反引号以原样输出
`-t 制定密码生成的字符集,
num:只使用数字[0-9],
char:只使用英文字母[a-zA-Z],
mix:使用数字和字母,
advance:使用数字、字母以及特殊字符`)
flag.Parse()
}
func GetRandomString() string{
rand.Seed(time.Now().UnixNano())
//初始化密码切片
var passwd []byte = make([]byte, length, length)
//源字符串
var sourceStr string
//判断字符类型,如果是数字
if charset == "num" {
sourceStr = NUmStr
//如果选的是字符
} else if charset == "char" {
sourceStr = CharStr
//如果选的是混合模式
} else if charset == "mix" {
sourceStr = NUmStr + CharStr
//如果选的是高级模式
} else if charset == "advance" {
sourceStr = CharStr + NUmStr + SpecStr
} else {
sourceStr = NUmStr
}
//遍历,生成一个随机index索引,
for i := 0; i < length; i++ {
index := rand.Intn(len(sourceStr))
passwd[i] = sourceStr[index]
}
return string(passwd)
}
func main() {
parseArgs()
passwd :=GetRandomString()
fmt.Println(passwd)
}
在windwos下编译Go to win,linux,mac
#生成exe
SET GOOS=windows
#生成linux SET GOOS=linux
#生成mac SET GOOS=darwin
SET GOARCH=amd64
cd 目录
go build 文件名.go