Skip to content

Instantly share code, notes, and snippets.

@darkcris1
Last active February 7, 2021 10:48
Show Gist options
  • Save darkcris1/6ff629704d149908ea7e12cf34709777 to your computer and use it in GitHub Desktop.
Save darkcris1/6ff629704d149908ea7e12cf34709777 to your computer and use it in GitHub Desktop.
Simple password generator
function generatePassword(length = 50) {
if (length < 5) throw new Error('Length must be minimum of 5')
const chars = 'abcdefghijklmopqrstnuvwxyz-[]{}()*-+,./\\1234567890'
let result = ''
for (let i = 0; i < length; i++) {
const randomChars = chars[Math.floor(Math.random() * chars.length)]
result += randomChars
}
return result
}
console.log(generatePassword()) // y.7xu2x+wcmo(ji/rzv]zsq6421c8/\zz{]pd31qvqyr1at.}p
console.log(generatePassword(10)) // (v*2x}w1cd
console.log(generatePassword(5)) // 0auy-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment