Skip to content

Instantly share code, notes, and snippets.

@inferno7291
Last active September 19, 2016 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inferno7291/3e953535081b6c00a97356ef2ac9ed29 to your computer and use it in GitHub Desktop.
Save inferno7291/3e953535081b6c00a97356ef2ac9ed29 to your computer and use it in GitHub Desktop.
Generate password random JS/PHP
#JS
function generatePasswordRand(length,type) {
switch(type){
case 'num':
characters = "0123456789";
break;
case 'alf':
characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
break;
case 'rand':
//FOR ↓
break;
default:
characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
break;
}
var pass = "";
for (i=0; i < length; i++){
if(type == 'rand'){
pass += String.fromCharCode((Math.floor((Math.random() * 100)) % 94) + 33);
}else{
pass += characters.charAt(Math.floor(Math.random()*characters.length));
}
}
return pass;
}
#PHP
//Generar una contraseña aleatoria
function generatePassword($longitud = 6,$tipo = ''){
switch ($tipo) {
case 'num':
$cadena="/\D+/";
break;
case 'alf':
$cadena="/\d+/";
break;
default:
$cadena="/\W+/";
break;
}
return substr(preg_replace($cadena, "", md5(rand())).preg_replace($cadena, "", md5(rand())).preg_replace($cadena, "", md5(rand())), 0, $longitud);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment