密码生成器

密码生成器

package main

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

var (
	NUmStr = "0123456789"
	ChanStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	SpecStr = "+=-@#~,.[]()!%^*$"
	MixtureStr = NUmStr+ChanStr+SpecStr
)

var (
	lens int
	num bool
	char bool
	mixture bool
)

func CommArgs(){
	flag.IntVar(&lens,"len",6,"default len 5")
	flag.BoolVar(&num,"num",false,"default false")
	flag.BoolVar(&char,"char",false,"default false")
	flag.BoolVar(&mixture,"mixture",false,"default false")
}

func init(){
	CommArgs()
	flag.Parse()
	rand.Seed(time.Now().UnixNano())
}

func Getstr(b []byte,str string)string{
	for i,_:=range b{
		b[i] = str[rand.Intn(len(str))]

	}
	return string(b)
}

func Randstring(lens int)string{
	 b := make([]byte,lens)
	 var str string
	if num {
		str=Getstr(b,NUmStr)

	}else if char {
		str=Getstr(b,ChanStr)

	}else{
		str=Getstr(b,MixtureStr )
	}
	return  str
}

func main() {

	if !num  && !char && !mixture  {
		fmt.Println("comm args err")
		return
	}

	str:=Randstring(lens)
	fmt.Println(str)
}

posted @ 2019-09-05 10:57  Egrep  阅读(343)  评论(0编辑  收藏  举报