password generator node js code example
Example 1: javascript password generator
var Password = {
_pattern : /[a-zA-Z0-9_\-\+\.]/,
_getRandomByte : function()
{
if(window.crypto && window.crypto.getRandomValues)
{
var result = new Uint8Array(1);
window.crypto.getRandomValues(result);
return result[0];
}
else if(window.msCrypto && window.msCrypto.getRandomValues)
{
var result = new Uint8Array(1);
window.msCrypto.getRandomValues(result);
return result[0];
}
else
{
return Math.floor(Math.random() * 256);
}
},
generate : function(length)
{
return Array.apply(null, {'length': length})
.map(function()
{
var result;
while(true)
{
result = String.fromCharCode(this._getRandomByte());
if(this._pattern.test(result))
{
return result;
}
}
}, this)
.join('');
}
};
Example 2: JavaScript Password Generator
function generate() {
let password = "";
let length = +document.getElementById("length").value;
let lowerCase = document.getElementById("lowercase").checked;
let upperCase = document.getElementById("uppercase").checked;
let symbol = document.getElementById("symbols").checked;
let number = document.getElementById("numbers").checked;
if (lowerCase + upperCase + symbol + number <= 0)
return;
for (let i = 0; i < length; i++) {
const r = generater(0, 3);
if (lowerCase && r === 0) {
password += generateRandomLowerCase();
} else if (upperCase && r === 1) {
password += generateRandomUpperCase();
} else if (symbol && r === 2) {
password += generateRandomSymbol();
} else if (number && r === 3) {
password += generater(0, 9);
} else {
i--;
}
}
document.getElementById("result").textContent = password;
}
generate();
function generateRandomLowerCase() {
return String.fromCharCode(generater(97, 122));
}
function generateRandomUpperCase() {
return String.fromCharCode(generater(65, 90));
}
function generateRandomSymbol() {
const symbols = "~*$%@#^&!?*'-=/,.{}()[]<>";
return symbols[generater(0, symbols.length - 1)];
}
function generater(min = 0, max = 1) {
return Math.floor(Math.random() * (max + 1 - min) + min);
}
Example 3: javascript password generator
<input type='text' id='p'/><br/>
<input type='button' value ='generate' onclick='document.getElementById("p").value = Password.generate(16)'>