生成所有可能的n字符密码 [英] Generate all possible n-character passwords

查看:142
本文介绍了生成所有可能的n字符密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为学习Go练习的一部分,我正在编写一个简单的暴力破解密码。



要生成所有可能的双字符密码,在Python中使用字符AE,我会使用 itertools.product()

 来自itertools导入产品
用于产品('ABCDE',repeat = 2)中的排列:
打印排列

然而,我在Go中努力做到这一点。



其他问题似乎是关于排列,这不是我想要的 。尽管Python文档包含了函数的示例实现,但我不知道如何将 yield 转换为Go。



我想我应该提到两个限制:


  1. 我想要密码的长度是可变的。也就是说,我可能想要做8个字符的密码,或者6个字符,或者其他的东西。这意味着我们不能只嵌套n个循环。

  2. 我不希望一次将它们全部存储在内存中。


解决方案

你想要的基本上是 n元笛卡尔积。因此,对于所有需要Prod(设置,设置,设置)的3字符密码。这可以迭代构建。首先构造n-1产品,然后为每个产品和初始集的每个元素添加元素。因此,例如,所有2个字符的密码 - > 3个字符的密码,其中唯一有效的字符是'a'或'b'。

ab = {a,b} - > {(a,a),(a,b),(b,a),(b,b)}

返回nil
}

//将输入复制到初始产品集中 - 一组
//一个字符集
prod:= make([] string,len(输入))
for i,char:=范围输入{
prod [i] = string(char)
}

for i:= 1;我< N; i ++ {
//更大的产品应该是输入的大小乘以
的大小// n-1大小的产品
next:= make([] string,0,len (输入)* len(prod))

//为每个单词添加每个字符并将其添加到新的集合
for _,word:= range prod {
for _,char:=范围输入{
next = append(next,word + string(char))
}
}

prod = next
}

return prod
}

游乐场版本: http://play.golang.org/p/6LhApeJ1bv



应该指出,这个解决方案有很大的改进空间。如果要构造所有长度为6-18的密码,则为每个密码单独调用此方法将重新计算以前计算的组。我会留下写更好的版本给你。根据我向您展示的内容,修改代码以获取任意(n-m)ary产品并从中计算n-ary产品应该不会太困难。 (提示:考虑如何递归地做这件事)


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 Python, I would use itertools.product():

from itertools import product
for permutation in product('ABCDE', repeat=2):
  print permutation

However, I'm struggling to do this in Go.

Other questions seem to be about permutations, which isn't quite what I want. And while the Python docs include a sample implementation of the function, I don't know how to translate yield into Go.

I suppose I should mention two restrictions:

  1. I'd like the length of the password to be variable. That is, I may want to do 8-character passwords, or 6-character, or something else. This means we can't just nest n loops.
  2. I don't want to have all of them in memory at once.

解决方案

What you want is basically the n-ary cartesian product of a set with itself. So for all 3-character passwords you want Prod(set,set,set). This can be constructed iteratively. First construct the n-1 product, then for each product and each element of the initial set, add the element. So for instance all 2 character passwords -> 3 character passwords where the only valid characters are 'a' or 'b'.

"ab" = {a,b} -> {(a,a),(a,b),(b,a),(b,b)} -> {(a,a,a),(a,a,b),(a,b,a),(a,b,b),(b,a,a),(b,a,b),(b,b,a),(b,b,b)}

func NAryProduct(input string, n int) []string {
    if n <= 0 {
        return nil
    }

    // Copy input into initial product set -- a set of
    // one character sets
    prod := make([]string, len(input))
    for i, char := range input {
        prod[i] = string(char)
    }

    for i := 1; i < n; i++ {
        // The bigger product should be the size of the input times the size of
        // the n-1 size product
        next := make([]string, 0, len(input)*len(prod))

        // Add each char to each word and add it to the new set
        for _, word := range prod {
            for _, char := range input {
                next = append(next, word + string(char))
            }
        }

        prod = next
    }

    return prod
}

Playground version: http://play.golang.org/p/6LhApeJ1bv

It should be noted that there's a lot of room for improvement on this solution. If you want to construct all passwords of length, say, 6-18, calling this method independently for each one will recalculate previously computed sets. I'll leave writing the better version up to you. Given what I've shown you, it shouldn't be too difficult to modify the code to take an arbitrary (n-m)ary product and compute the n-ary product from it. (Hint: think about how you'd do this recursively)

这篇关于生成所有可能的n字符密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆