AS3 Create Random Password


/ Published in: ActionScript 3
Save to your folder(s)

Based upon this (https://gist.github.com/988479) but made a few fixes.


Copy this code and paste it in your HTML
  1. function createRandomPassword(hashLen:uint, includeLowercase:Boolean = true, includeNumbers:Boolean = true, includeUppercase:Boolean = false):String {
  2. var strHash:String = "";
  3. if (includeLowercase) strHash += "abchefghjkmnpqrstuvwxyz";
  4. if (includeNumbers) strHash += "0123456789";
  5. if (includeUppercase) strHash += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  6. var maskPick:Number;
  7. var passwordStr:String = "";
  8. var maskLen:uint = strHash.length;
  9. for (var i:uint = 0; i < hashLen; i++) {
  10. maskPick = Math.floor(Math.random() * maskLen);
  11. passwordStr += strHash.charAt(maskPick);
  12. }
  13. return passwordStr;
  14. }
  15.  
  16. trace(createRandomPassword(8));
  17. // Output
  18. // 6k3x10h8j
  19.  
  20. trace(createRandomPassword(6, true, true, true));
  21. // Output
  22. // D2jHEfT
  23.  
  24. trace(createRandomPassword(16, false, true, true));
  25. // Output
  26. // O8I2DTSLHHWRI50Z
  27.  
  28. trace(createRandomPassword(16, true, false, false));
  29. // Output
  30. // xxfeyshhqkrsqvhjt
  31.  
  32. trace(createRandomPassword(16, false, false, true));
  33. // Output
  34. // XMXIDTIXMNGNUXZT
  35.  
  36. trace(createRandomPassword(16, false, true, false));
  37. // Output
  38. // 4026352375069424

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.