随机密码

随机密码_第1张图片

一.Go实现随机密码

  • 随机密码
package main

import (
    "fmt"
    "math/rand"
    "os"
    "strconv"
    "time"
)

func RandomPassword(num int)   {
    length := num
    if len(os.Args) > 1 {
        arg := os.Args[1]
        i, err := strconv.ParseInt(arg, 10, 64)
        if err != nil {
            fmt.Println("参数转换失败")
            return
        }
        if i < 4 || i > 30 {
            fmt.Println("密码长度介于4~30之间")
            return
        }
        length = int(i)
    }
    baseStr := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    baseSymbol := "!@#$%^&*+-_=."
    fmt.Println("-----简单密码-----")
    for i := 0; i < 5; i++ {
        fmt.Println(getRandStr(baseStr, length))
    }
    fmt.Println("-----复杂密码-----")
    for i := 0; i < 5; i++ {
        fmt.Println(getRandStr(baseStr+baseSymbol, length))
    }
}

func getRandStr(baseStr string, length int) string {
    r := rand.New(rand.NewSource(time.Now().UnixNano() + rand.Int63()))
    bytes := make([]byte, length)
    l := len(baseStr)
    for i := 0; i < length; i++ {
        bytes[i] = baseStr[r.Intn(l)]
    }
    return string(bytes)
}

func main() {
    RandomPassword(25)
}

你可能感兴趣的:(go)