Skip to content

Instantly share code, notes, and snippets.

@james-jlo-long
Last active March 22, 2017 10:27
Show Gist options
  • Save james-jlo-long/8cfcd2cebbd858cbad7b1b11b110dd93 to your computer and use it in GitHub Desktop.
Save james-jlo-long/8cfcd2cebbd858cbad7b1b11b110dd93 to your computer and use it in GitHub Desktop.
A couple of functions to generate simple passwords. Beware that this is based on Math.random() - it is not cryptographically secure
var PW = (function () {
"use strict";
var sym = "!£$%^&*()-_=+[{]};:@#~,<.>/?\\|";
var getRand = (n) => Math.floor(Math.random() * n);
var basic = () => (
btoa(getRand(Date.now()))
.replace(/=+$/, "")
.split("")
.reverse()
.join("")
.slice(0, -2)
);
var strong = function () {
var pw = basic().split("");
var times = getRand(pw.length);
while (times) {
times -= 1;
if (Math.random() < 0.5) {
pw[getRand(pw.length)] = sym[getRand(sym.length)];
}
}
return pw.join("");
};
return {
basic,
strong
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment