How to generate possible password combinations fro

2019-03-06 20:45发布

I am trying to generate a list of strings (possible passwords) that I can then hash (SHA-256) to compare to an already existing password hashes that I have.

I know that the password must be a popular boys or girls name, with no specified case, followed by a number ranging from 0 to 9999, e.g BoB167, AliCE1000, conNor99, tHe9329

How can I go about generating the possible password strings that the password can be so that I can then hash them to compare with my hashes

Does anyone know where I can start I was thinking to just create a method that takes in all possible characters add every combination to a string but im unsure as to how to do this

1条回答
三岁会撩人
2楼-- · 2019-03-06 21:06

Your question is a little bit too broad, but since you indicated in the comments that your issue is coming up with the lowercase/uppercase combinations of the names, I'll just address that part in my answer.

  • For a given name with n characters, there are 2n possible combinations of upper and lowercase letters.
    E.g. for bob there are 23 = 8 possible combinations.

  • Looping from 0 to 2n will yield 2n consecutive integers.
    E.g. for bob, that would be 0, 1, 2, 3, 4, 5, 6 and 7.

  • The bit patterns of those integers can be used to determine whether a character needs to be uppercase or lowercase.
    E.g. for bob, the bit patterns would be 000, 001, 010, 011, and so on.
  • You can then use a little bit of bit shifting magic (borrowed from here) to determine whether the nth bit in an integer is set to generate the actual name combinations.

Example code

public class Test {
    public static void main(String[] args) {
        combinations("bob");
    }

    private static void combinations(String name) {
        char[] chars  = name.toCharArray();

        for (int i = 0; i < Math.pow(2, chars.length); i++) {
            char[] result = new char[chars.length];

            for (int n = 0; n < chars.length; n++) {
                result[n] = isBitSet(i, n) ? Character.toUpperCase(chars[n])
                                           : Character.toLowerCase(chars[n]);
            }

            System.out.println(result);
        }
    }

    private static boolean isBitSet(int i, int n) {
        return ((i & (1 << n)) != 0);
    }
}

Output

This yields the following output:

bob
Bob
bOb
BOb
boB
BoB
bOB
BOB
查看更多
登录 后发表回答