Created
March 23, 2020 21:58
-
-
Save DavidWells/defd14cb196f766cbd86a077afb31615 to your computer and use it in GitHub Desktop.
Generate random password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-={}[]:;<>?/|\\'; | |
function generatePassword(length = 12) { | |
let pass = ''; | |
for (let i = length; i > 0; --i) pass += CHARS[Math.floor(Math.random() * CHARS.length)]; | |
return pass | |
} | |
// Usage: | |
generatePassword(15) | |
// > "hBI!A31Hs}(oIv*" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't mean to intrude, but I just wanted to chime in that the password security, when derived from
Math.random()
, is low:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random