Complete Random Password Generation Algorithm
Hi Friends,
Few days back I had a requirement of a routine/method which will generate a completely random password but a very strong password too.
Requirements : -
Generated password must contain
a) At least one Capital letter (i.e. A to Z - ASCII value- 65 to 90)
b) At least one special character (from set of - '@','!','#','$','^','&','*','~')
c) At least one numeric character (i.e. 0 to 9 - ASCII value - 48 to 57)
d) and remaining characters to small letters (i.e. a to z - ASCII value - 97-122)
There positions to be random and each time it should output a random, different, distinct password.
So here is my code implementation - (in JAVA)
But logic can be changed easily for any language.
Code :
Few days back I had a requirement of a routine/method which will generate a completely random password but a very strong password too.
Requirements : -
Generated password must contain
a) At least one Capital letter (i.e. A to Z - ASCII value- 65 to 90)
b) At least one special character (from set of - '@','!','#','$','^','&','*','~')
c) At least one numeric character (i.e. 0 to 9 - ASCII value - 48 to 57)
d) and remaining characters to small letters (i.e. a to z - ASCII value - 97-122)
There positions to be random and each time it should output a random, different, distinct password.
So here is my code implementation - (in JAVA)
But logic can be changed easily for any language.
Code :
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.DigestInputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Random;
/**
* @author Pramod Khare
* Purpose : To generate Random password with required strength
*/
public class Main {
public static void main(String arg[]) throws Exception {
System.out.println("Password = " + generateResetPassword());
}
public static String generateResetPassword() {
String finalPwd = null;
char[] num;
char[] spchar;
char[] caps;
Random rand = new Random();
int totalLength = rand.nextInt(5) + 8;
int spcharlen = rand.nextInt(2) + 1; // Max - 2 , Min -1
int capslen = rand.nextInt(2) + 1; // Max - 2 , Min -1
int numlen = rand.nextInt(2) + 1; // Max - 2 , Min -1
int extracharlen = spcharlen + capslen + numlen;
int[] capsPOS = new int[capslen]; // Position index array
int[] spcharPOS = new int[spcharlen];
int[] numPOS = new int[numlen];
// calculate indexes for chars
for (int i = 0; i < capslen; i++) {
int temp = (rand.nextInt(totalLength - 1) + 1);
for (int t = 0; t < i; t++) {
if (temp == capsPOS[t]) {
temp = (rand.nextInt(totalLength - 1) + 1);
t = -1;
}
}
capsPOS[i] = temp; // from 1 to n-1
}
for (int i = 0; i < spcharlen; i++) {
int temp = (rand.nextInt(totalLength - 1) + 1);
for (int j = 0, t = 0; capslen > j || t < i; j++, t++) {
if ((t < i && temp == spcharPOS[t])
|| (capslen > j && temp == capsPOS[j])) {
temp = (rand.nextInt(totalLength - 1) + 1);
j = -1;
t = -1;
}
}
spcharPOS[i] = temp;
}
for (int i = 0; i < numlen; i++) {
int temp = (rand.nextInt(totalLength - 1) + 1);
for (int j = 0, k = 0, t = 0; (capslen > j || k < spcharlen || t < i); j++, k++, t++) {
if ((j < capslen && temp == capsPOS[j])
|| (k < spcharlen && temp == spcharPOS[k])
|| (t < i && temp == numPOS[t])) {
temp = (rand.nextInt(totalLength - 1) + 1);
j = -1;
k = -1;
t = -1;
}
}
numPOS[i] = temp;
}
char[] spchars = { '@', '!', '#', '$', '^', '&', '*', '~' }; // 0-7 = 8
// a-z - 97-122
// A-Z - 65-90
// 0-9 - 48-57
// Generate Random Characters
char[] extrachars = new char[extracharlen]; // hold actual chars
int[] extracharsPOS = new int[extracharlen]; // hold there positions
int e = 0;
for (int i = 0; i < capslen; i++) {
extrachars[e] = (char) (rand.nextInt(26) + 65);
extracharsPOS[e++] = capsPOS[i];
}
for (int i = 0; i < spcharlen; i++) {
extrachars[e] = spchars[rand.nextInt(8)];
extracharsPOS[e++] = spcharPOS[i];
}
for (int i = 0; i < numlen; i++) {
extrachars[e] = (char) (rand.nextInt(10) + 48);
extracharsPOS[e++] = numPOS[i];
}
System.out.println("Total Extra Chars = " + e);
for (int i = 0; i < extracharsPOS.length; i++) {
System.out.println("POS - " + extracharsPOS[i] + "\t Char - "
+ extrachars[i]);
}
// To do - randomise char order too.. currently caps,spachars,nums is
// fixed order,
// randomise it according to their actual positions
// While sorting change char positions too
// Arrays.sort(extracharsPOS);
// For Complete Randomness of chars -
int temp = 0;
for (int i = 0; i < extracharsPOS.length; i++) {
for (int j = (i + 1); j < extracharsPOS.length; j++) {
if (extracharsPOS[i] > extracharsPOS[j]) {
temp = extracharsPOS[i];
extracharsPOS[i] = extracharsPOS[j];
extracharsPOS[j] = temp;
// Changing Value Array positions accordingly too
temp = (int) extrachars[i];
extrachars[i] = extrachars[j];
extrachars[j] = (char) temp;
}
}
}
System.out.println("totalLength = " + totalLength);
System.out.println("capslen = " + capslen);
System.out.println("spcharlen = " + spcharlen);
System.out.println("numlen = " + numlen);
String pwd = "";
for (int m = 0, f = 0; m < totalLength; m++) {
if (f < extrachars.length && extracharsPOS[f] == m) {
pwd += extrachars[f];
f++;
} else {
pwd += (char) (rand.nextInt(26) + 97);
}
}
return pwd;
}
}
Comments
Post a Comment