26Jan/070
Creating Random Password in JAVA
Sometime, we need to create a random string for some purposes. Well, I do need this random string for a project of my friend. So, how do I create this random string? The idea is simple, I just need to create this random string from MD5 hashes that came from current time and multiply it by random integer and multiply again by some integer.
MD5 md5 = new MD5();
Date date = new Date();
long seed = date.getTime();
seed = seed * (1 + (int)(Math.random() * 5000));
md5.Update(String.valueOf(seed));
hash = md5.asHex().toUpperCase();
int startIndex = 1 + (int)(Math.random() * 50);
if(((startIndex + passwordLength) - 1) > hash.length()){
startIndex = hash.length() - passwordLength;
}
returnValue = hash.substring(startIndex - 1, (startIndex - 1) + passwordLength);
As For the MD5, I use a library from FastMD5. And this is my code. Well, it's a non optimized code. But, you should get the point.
/*
* RandomPasswordGenerator.java
*/
import com.twmacinta.util.*;
import java.util.*;
/**
* Create random password from md5 hashes
* @author Arief Bayu Purwanto
*/
public class RandomPasswordGenerator {
private int passwordLength = 0;
private boolean debug = false;
/**
* Creates a new instance of RandomPasswordGenerator
* and set it's passwordLength to 5;
*
*/
public RandomPasswordGenerator() {
passwordLength = 5;
}
/**
* Creates a new instance of RandomPasswordGenerator
* @param pl set password length. If pl <= 0 then pl is set to 5
*/
public RandomPasswordGenerator(int pl){
setPasswordLength(pl);
}
/**
* Creates a new instance of RandomPasswordGenerator
* @param pl set password length. If pl <= 0 then pl is set to 5
* @param dbg set debug mode
*/
public RandomPasswordGenerator(int pl, boolean dbg){
setPasswordLength(pl);
setDebug(dbg);
}
/**
* Generate the password
*/
public String generate(){
String returnValue = "";
String hash = "";
MD5 md5 = new MD5();
Date date = new Date();
long seed = date.getTime();
seed = seed * (1 + (int)(Math.random() * 5000));
md5.Update(String.valueOf(seed));
hash = md5.asHex().toUpperCase();
int startIndex = 1 + (int)(Math.random() * 50);
if(((startIndex + passwordLength) - 1) > hash.length()){
startIndex = hash.length() - passwordLength;
}
if(debug){
System.out.println("=====================================");
System.out.println("Seed : " + seed);
System.out.println("Hash : " + hash);
System.out.println("Hash Length : " + hash.length());
System.out.println("Start Index : " + startIndex);
System.out.println("PasswordLength : " + passwordLength);
}
returnValue = hash.substring(startIndex - 1, (startIndex - 1) + passwordLength);
if(debug){
System.out.println("Return Value : " + returnValue);
System.out.println("=====================================");
}
return returnValue;
}
/**
* Set password length
* @param pl the password length. If pl <= 0 then pl is set to 5
*/
public void setPasswordLength(int pl){
if(pl <=0){
passwordLength = 5;
}else{
passwordLength = pl;
}
}
/**
* set tebug mode
* @param _debug debug value
*/
public void setDebug(boolean _debug){
this.debug = _debug;
}
}








