Last active
August 12, 2018 12:27
-
-
Save toandv/1c4fe2162fef45cd650e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RandomPasswordGenerator { | |
private RandomPasswordGenerator() { | |
} | |
private static final char[] LOWERCASES = createCharArray('a', 'z'); | |
private static final char[] UPPERCASES = createCharArray('A', 'Z'); | |
private static final char[] DIGITS = createCharArray('0', '9'); | |
private static final SecureRandom RANDOM = new SecureRandom(); | |
public static final String getRandomPassword(int length) { | |
if (length < 0) { | |
throw new IllegalArgumentException("Negative length: " + length); | |
} | |
char[] pa = new char[length]; | |
for (int i = 0; i < length; i++) { | |
int d = RANDOM.nextInt(3); | |
if (d == 0) { | |
pa[i] = LOWERCASES[RANDOM.nextInt(26)]; | |
} else if (d == 1) { | |
pa[i] = UPPERCASES[RANDOM.nextInt(26)]; | |
} else if (d == 2) { | |
pa[i] = DIGITS[RANDOM.nextInt(10)]; | |
} | |
} | |
return new String(pa); | |
} | |
private static char[] createCharArray(int start, int end) { | |
char[] ar = new char[end - start + 1]; | |
for (int i = 0; i < end - start + 1; i++) { | |
ar[i] = (char) (start + i); | |
} | |
return ar; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment