0

I would like to generate a random password respecting the following format:

  • at least one uppercase character
  • at least one lowercase character
  • at least one digit
  • the length should be at least 6 characters

I thought about it and the only thing I could think of is some function that is quite long and ugly. Could you guys offer me a simpler, more efficient way of doing this?

PS: I'm using this function not on the client side, but on the server side, on my Node.js web application.

Teo
  • 3,394
  • 11
  • 43
  • 73
  • 1
    It's totally pointless to generate passwords with client side scripting. The end user can see the code and get the password himself. – Shadow The GPT Wizard Dec 31 '13 at 11:44
  • It doesn't neccessarely follow your specific rules, but `require('crypto').randomBytes(10)` etc. comes to mind ? – adeneo Dec 31 '13 at 12:16
  • You have to write your own function for it. I think the following answer can help you to write it: http://stackoverflow.com/questions/17088798/javascript-node-js-specific-random-string-builder – Workonphp Dec 31 '13 at 11:55

3 Answers3

7

Here is my solution:

/**
 * sets of charachters
 */
var upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
var lower = 'abcdefghijklmnopqrstuvwxyz'
var digit = '0123456789'
var all = upper + lower + digit

/**
 * generate random integer not greater than `max`
 */

function rand (max) {
  return Math.floor(Math.random() * max)
}

/**
 * generate random character of the given `set`
 */

function random (set) {
  return set[rand(set.length - 1)]
}

/**
 * generate an array with the given `length` 
 * of characters of the given `set`
 */

function generate (length, set) {
  var result = []
  while (length--) result.push(random(set))
  return result
}

/**
 * shuffle an array randomly
 */
function shuffle (arr) {
  var result = []

  while (arr.length) {
    result = result.concat(arr.splice(rand[arr.length - 1]))
  }

  return result
}
/**
 * do the job
 */
function password (length) {
  var result = [] // we need to ensure we have some characters

  result = result.concat(generate(1, upper)) // 1 upper case
  result = result.concat(generate(1, lower)) // 1 lower case
  result = result.concat(generate(1, digit)) // 1 digit
  result = result.concat(generate(length - 3, all)) // remaining - whatever

  return shuffle(result).join('') // shuffle and make a string
}

console.log(password(6))
vkurchatkin
  • 13,364
  • 2
  • 47
  • 55
1

How about we place the formats into strings and we can place than into an array, then randomly chose one and randomly chose and item in that sub-array item:

var uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lowers = "abcdefghijklmnopqrstuvwxyz";
var digits = "01234567890";
var all = uppers + lowers + digits;
var choices = [uppers,lowers,digits];
var checks = [];

var password = ""; 
var ranLength = Math.ceil(Math.random()*10)+3;
for(var i=0; i<ranLength; i++){
    var choice = choices[Math.ceil(Math.random()*3)-1];
    var choiceItem = choice[Math.ceil(Math.random()*(choice.length))-1]
    password += choiceItem;
}

for(var i=0; i<3; i++){ // Append needed values to end
    var choice = choices[i]; 
    var choiceItem = choice[Math.ceil(Math.random()*(choice.length))-1] 
    password += choiceItem;
}

password = password.split('').sort(function(){
    return 0.5 - Math.random(); 
}).join('');

alert(password);

Edited: Sorry made a small mistake. Fixed.

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • Your method is not 100% accurate(e.g I got 00dkb8b which does not respect the format) because there is a chance that the random will not get all the choices (i.e uppers AND lowers AND digits). Therefore you should test the resulted string with the appropriate regex. Here's the fiddle: http://jsfiddle.net/DwhuW/. Update your answer and I'll mark it as the correct one. – Teo Dec 31 '13 at 12:14
  • it's bad to check with regexp until it is ok, it can take significant time. checkout my solution – vkurchatkin Dec 31 '13 at 12:17
  • I agree regexp isn't needed. I was planing on assigning indexes(0,1,2) and place them randomly into the password after generation so there will always be an uppercase, lowercase, and digit. I'll update it in a few minutes. – Spencer Wieczorek Dec 31 '13 at 12:37
-2

Try this...

var randomstring = Math.random().toString(36).slice(-8);
Rahul Sahu
  • 274
  • 1
  • 4
  • 15