Posted By

Pjotor on 10/19/10


Tagged

password security


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

martins


Keyboard-friendly passwords


 / Published in: JavaScript
 

Generates keyboard-friendly passwords. Use: pwd.gen(7,1) (generate a 7 char password with 1 special character)

  1. /*
  2.  * pwd - Generate keyboard-friendly passwords
  3.  * base: base chars
  4.  * chars: basechars with anexing keys
  5.  * special: special chars
  6.  * gen: (function) generates password with arguments:
  7.  * len: length of password
  8.  * spec: number of special characters
  9.  * retruns: string of length len
  10.  *
  11.  * r: (function) returns a random number with arguments:
  12.  * max: max number
  13.  * min: min number
  14.  */
  15.  
  16. var pwd = {
  17. base : "1234567890qwertyuiopasdfghjklzxcvbnm",
  18. chars : {
  19. c1 : ["2","q"], c2 : ["1","q","w","3"], c3 : ["2","w","e","4"],
  20. c4 : ["3","e","r","5"], c5 : ["4","r","t","6"], c6 : ["5","t","y","7"],
  21. c7 : ["6","y","u","8"], c8 : ["7","u","i","9"], c9 : ["8","i","o","0"],
  22. c0 : ["9","o","p"], cq : ["1","2","w","s","a"],
  23. cw : ["2","3","e","s","a","q"], ce : ["3","4","r","d","s","w"],
  24. cr : ["4","5","t","f","d","e"], ct : ["5","6","y","g","f","r"],
  25. cy : ["6","7","u","h","g","t"], cu : ["7","8","i","j","h","y"],
  26. ci : ["8","9","o","k","j","u"], co : ["9","0","p","l","k","i"],
  27. cp : ["0","l","o"], ca : ["q","w","s","z"], cs : ["w","e","d","x","z","a"],
  28. cd : ["e","r","f","c","x","s"], cf : ["r","t","g","v","c","d"],
  29. cg : ["t","y","h","b","v","f"], ch : ["y","u","j","n","b","g"],
  30. cj : ["u","i","k","m","n","h"], ck : ["i","o","l","m","j"],
  31. cl : ["o","p","k"], cz : ["a","s","x"], cx : ["s","d","c","z"],
  32. cc : ["d","f","v","x"], cv : ["f","g","b","c"], cb : ["g","h","n","v"],
  33. cn : ["h","j","m","b"], cm : ["j","k","n"]
  34. },
  35. special : ["!","@","#","$","%","&","/","(",
  36. ")","=","?","+",".",",",":",";","_","-"," "],
  37. gen : function(len,spec){
  38. var xs=[]; var xi,xr;
  39. for(xi=0; xi<(len-spec); xi++) {
  40. if(xs.length<1) { xs[xs.length] = pwd.base.substr(pwd.r(pwd.base.length),1);
  41. continue; }
  42. xr = pwd.r(pwd.chars["c"+xs[xi-1]].length);
  43. xs[xs.length] = pwd.chars["c"+xs[xi-1]][xr];
  44. }
  45. for(xi=0; xi<spec; xi++){ xr = pwd.r(xs.length);
  46. xs[xr] = xs[xr].toUpperCase();
  47. xs.splice(xr,0,pwd.special[pwd.r(pwd.special.length)]); }
  48. return xs.join("");
  49. },
  50. r : function(max,min) {
  51. min = min || 0;
  52. return min + Math.floor(Math.random()*((max-min)));
  53. }
  54. }

Report this snippet  

You need to login to post a comment.