Generate all possible n-character passwords

前端 未结 2 984
说谎
说谎 2021-02-15 23:04

As part of a learning-Go exercise, I\'m writing a simplistic brute-force password cracker.

To generate all possible 2-character passwords that use the characters A-E in

2条回答
  •  被撕碎了的回忆
    2021-02-15 23:54

    For example, satisfying your restrictions,

    package main
    
    import "fmt"
    
    func nextPassword(n int, c string) func() string {
        r := []rune(c)
        p := make([]rune, n)
        x := make([]int, len(p))
        return func() string {
            p := p[:len(x)]
            for i, xi := range x {
                p[i] = r[xi]
            }
            for i := len(x) - 1; i >= 0; i-- {
                x[i]++
                if x[i] < len(r) {
                    break
                }
                x[i] = 0
                if i <= 0 {
                    x = x[0:0]
                    break
                }
            }
            return string(p)
        }
    }
    
    func main() {
        np := nextPassword(2, "ABCDE")
        for {
            pwd := np()
            if len(pwd) == 0 {
                break
            }
            fmt.Println(pwd)
        }
    }
    

    Output:

    AA
    AB
    AC
    AD
    AE
    BA
    BB
    BC
    BD
    BE
    CA
    CB
    CC
    CD
    CE
    DA
    DB
    DC
    DD
    DE
    EA
    EB
    EC
    ED
    EE
    

提交回复
热议问题