Last active
September 19, 2016 14:56
-
-
Save inferno7291/3e953535081b6c00a97356ef2ac9ed29 to your computer and use it in GitHub Desktop.
Generate password random JS/PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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