Java : Human readable password generator
Below is a Java version of my previous tutorial on how to generate human readable password. Basically, what this Java program does is to generate a password that look like this "mu27ju" instead of "sdjfasf12gax3ad@mjxsof".
This kind of password is easier to remember when read by text-to-speech software over the phone.
For the best result, keep the number of alphabets to 6 and digits to 2. It should be practical and random enough. Anything beyond that will be hard to read and remember.
Here you go!
package socketloop;
import java.security.SecureRandom;
public class HumanReadablePasswordGenerator {
private static final String VOWELS = "aeiou";
private static final String CONSONANTS = "bcdfghjklmnpqrstvwxyz";
private static final String DIGITS = "0123456789";
public static void main(String[] args) {
System.out.format("Password length of 6 with 2 digits in the middle : %s%n", humanReadablePassword(6,2)); // best result
System.out.format("Password length of 12 with 2 digits in the middle : %s%n", humanReadablePassword(12,2));
}
public static String humanReadablePassword(int alphabetSize, int numberSize) {
// sanity check
if (alphabetSize < 1) throw new IllegalArgumentException();
if (numberSize < 1) throw new IllegalArgumentException();
// initialize
char randomCharacter = 'a';
int character = 0;
int prefixSize = alphabetSize/2;
if (alphabetSize%2 != 0) {
prefixSize = ((int)(alphabetSize/2)) + 1;
}
StringBuilder prefixPart = new StringBuilder(prefixSize);
for (int i = 0; i < prefixSize-1; i++) {
if (i%2 == 0) {
// use consonants
character = (int)(Math.random()*CONSONANTS.length());
randomCharacter = CONSONANTS.charAt(character);
prefixPart.append(randomCharacter);
} else {
// use vowels
character = (int)(Math.random()*VOWELS.length());
randomCharacter = VOWELS.charAt(character);
prefixPart.append(randomCharacter);
}
}
StringBuilder middlePart = new StringBuilder(numberSize);
for (int i = 0; i < numberSize; i++) {
// use digits
character = (int)(Math.random()*DIGITS.length());
randomCharacter = DIGITS.charAt(character);
middlePart.append(randomCharacter);
}
int suffixSize = alphabetSize - prefixSize;
StringBuilder suffixPart = new StringBuilder(suffixSize);
for (int i = 0; i < suffixSize-1; i++) {
if (i%2 == 0) {
// use consonants
character = (int)(Math.random()*CONSONANTS.length());
randomCharacter = CONSONANTS.charAt(character);
suffixPart.append(randomCharacter);
} else {
// use vowels
character = (int)(Math.random()*VOWELS.length());
randomCharacter = VOWELS.charAt(character);
suffixPart.append(randomCharacter);
}
}
prefixPart.append(middlePart).append(suffixPart);
return prefixPart.toString();
}
}
Sample output:
Password length of 6 with 2 digits in the middle : mu72ne
Password length of 12 with 2 digits in the middle : yucer20zosuy
See also : Golang : Generate human readable password
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.3k Golang : Calculate diameter, circumference, area, sphere surface and volume
+10.3k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+9.7k Golang : Check if user agent is a robot or crawler example
+7.1k Golang : Scanf function weird error in Windows
+6.6k Golang : Get expvar(export variables) to work with multiplexer
+28.4k Golang : Detect (OS) Operating System
+5k Golang : Print instead of building pyramids
+8.8k Golang : Intercept and compare HTTP response code example
+5.8k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+40.7k Golang : How to check if a string contains another sub-string?
+48.2k Golang : Upload file from web browser to server
+10.3k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?