So I know generating a password in the following way is a bad idea. I'd say it has only a few (like maybe 5 or so) bits of entropy, but I'm unable to calculate it properly.
Can someone show me, how to calculate the average amount of tries needed to guess a password of length n generated in the following way using Oracle's JDK 7?
I assume the relevant factors are:
- alphabet size (62 - 5 for restricting confusing-looking characters),
- two step process to select character class and then character,
- rounding to integer,
- try-until-succeed way of sampling the characters,
- intrinsic properties of Math.random().
But I can't get the exact numbers.
char[] generate(int n) {
char[] pw = new char[n];
for (int i = 0; i < n; i++) {
int c;
while (true) {
c = randomCharacter(c);
if (c == '0' || c == 'O' || c == 'I' || c == '1' || c == 'l')
continue;
else
break;
}
pw[i] = (char) c;
}
return pw;
}
int randomCharacter(int c) {
switch ((int) (Math.random() * 3)) {
case 0:
c = '0' + (int) (Math.random() * 10);
break;
case 1:
c = 'a' + (int) (Math.random() * 26);
break;
case 2:
c = 'A' + (int) (Math.random() * 26);
break;
}
return c;
}