Javascript: Sistema de Password
Neste tutorial vou ensinar como fazer um sistema que cria uma password automaticamente.
Vamos criar uma função que vai gerar a nossa password que irá conter caracteres ao calhas utilizando o método charAt() que serve para seleccionar um caracter isolado numa cadeia de caracteres.
O formulário
Vamos primeiro criar um formulário, como isto é um tutorial geral vou colocar vários campos para poderem compreender o controlo da criação da password.
<form name="form1">
O primeiro caracter pode ser:
<input type=checkbox name=firstNumber checked>
Numero
<input type=checkbox name=firstLower checked>
Lowercase
<input type=checkbox name=firstUpper checked>
Uppercase
<input type=checkbox name=firstOther>
Outro<br />
Os restante caracteres podem ser:
<input type=checkbox name=latterNumber checked>
Numero
<input type=checkbox name=latterLower checked>
Lowercase
<input type=checkbox name=latterUpper checked>
Uppercase
<input type=checkbox name=latterOther>
Outro
<br />
Comprimento da password:
<input type=text name=passwordLength value="8" size=3>
<br />
Caracteres extra:
<input type=text name=extraChars size=20>
<br />
Password:
<input type=text name=password size=20>
<br>
<input type=button value="Criar password" onClick="document.form1.password.value =
getPassword(document.form1.passwordLength.value, document.form1.extraChars.value,
document.form1.firstNumber.checked, document.form1.firstLower.checked,
document.form1.firstUpper.checked, document.form1.firstOther.checked,
document.form1.latterNumber.checked, document.form1.latterLower.checked,
document.form1.latterUpper.checked, document.form1.latterOther.checked);">
</form>
Vamos ver como está o nosso formulário: exemplo1.html
A função
A função vai ler os dados do formulário, e vai criar uma password com letras e/ou números ao calhas.
function getRandomNum(lbound, ubound) {
return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}
function getRandomChar(number, lower, upper, other, extra) {
var numberChars = "0123456789";
var lowerChars = "abcdefghijklmnopqrstuvwxyz";
var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:’\",<.>/? ";
var charSet = extra;
if (number == true)
charSet += numberChars;
if (lower == true)
charSet += lowerChars;
if (upper == true)
charSet += upperChars;
if (other == true)
charSet += otherChars;
return charSet.charAt(getRandomNum(0, charSet.length));
}
function getPassword(length, extraChars, firstNumber, firstLower, firstUpper, firstOther,
latterNumber, latterLower, latterUpper, latterOther) {
var rc = "";
if (length > 0)
rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
for (var idx = 1; idx < length; ++idx) {
rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
}
return rc;
}
Podem ver um exemplo funcional aqui: index.html
Download dos ficheirosa: passgen.zip
Referências:
- http://muiomuio.net/wp-content/passgen/
- http://www.truquesedicas.com/tutoriais/javascript/00017a.htm
- http://www.w3schools.com/jsref/jsref_charAt.asp
3 Comments
Comments RSS TrackBack Identifier URI
Leave a comment









































Apesar de ainda não ser...
[...] Podem visitar o tutorial aqui: http://muiomuio.net/javascript-sistema-de-password/ Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages. [...]
Muito interessante, valeu a dica!!!!
[...] usar o gerador de passwords que criei neste artigo para criar uma password. Link: [...]