Skip to content

Instantly share code, notes, and snippets.

@russianryebread
Created December 15, 2014 16:01
Show Gist options
  • Save russianryebread/90c5ab57005fcde3bf5d to your computer and use it in GitHub Desktop.
Save russianryebread/90c5ab57005fcde3bf5d to your computer and use it in GitHub Desktop.
Random Password Generator
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Generate Secure Password</title>
<script type="text/javascript">
var specials = '!@$%^&*()_+{}:<>?\|[];,./~';
var lowercase = 'abcdefghijklmnopqrstuvwxyz';
var uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var numbers = '0123456789';
var all = specials + lowercase + uppercase + numbers;
String.prototype.pick = function(min, max) {
var n, chars = '';
if (typeof max === 'undefined') {
n = min;
} else {
n = min + Math.floor(Math.random() * (max - min));
}
for (var i = 0; i < n; i++) {
chars += this.charAt(Math.floor(Math.random() * this.length));
}
return chars;
};
// Credit to @Christoph: http://stackoverflow.com/a/962890/464744
String.prototype.shuffle = function() {
var array = this.split('');
var tmp, current, top = array.length;
if (top) while (--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array.join('');
};
window.addEventListener("load", function(el){
var password = (specials.pick(1) + lowercase.pick(1) + uppercase.pick(1) + all.pick(12, 12)).shuffle();
var pw_el = document.getElementById("password");
// Set the initial password on page load, and select it.
pw_el.value = password;
pw_el.select();
// allow for copying
pw_el.addEventListener("click", function(e){
this.select();
}, false);
// Set a new password on page click.
var pw_bt = document.getElementById("password_bt");
pw_bt.addEventListener("click", function(e){
password = (specials.pick(1) + lowercase.pick(1) + uppercase.pick(1) + all.pick(12, 12)).shuffle();
pw_el.value = password;
pw_el.select();
}, false);
}, false);
</script>
<style type="text/css">
body {
font-family: "Helvetica Neue";
}
button {
padding: 10px 20px;
border: 1px solid black;
border-radius: 5px;
background: transparent;
font-size: 15pt;
font-weight: 100;
}
button:focus {
outline: none;
}
button:hover {
background: #000;
color: #fff;
cursor: pointer;
}
input {
font-size: 15pt;
font-weight: 100;
padding: 10px;
width: 500px;
}
</style>
</head>
<body>
<h1>CCWIS Secure Password Generator</h1>
<input type="text" readonly="readonly" id="password">
<button accesskey="r" id="password_bt">Generate New Password</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment