Last active
August 29, 2015 14:04
-
-
Save gabrielcesar/59981583ef1251e51bef to your computer and use it in GitHub Desktop.
Generates Tokens in JavaScript.
This file contains hidden or 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
<!DOCTYPE html> | |
<!-- | |
Exemplo de uso da Função token (). | |
Arquivo: token.js | |
--> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
<title>Token</title> | |
<script src="token.js"></script> | |
</head> | |
<body> | |
<input type="button" onclick="alert ( token ( 8 ))" value="Gerar TOKEN de 8 caracteres" /> | |
<input type="button" onclick="alert ( token ( 16 ))" value="Gerar TOKEN de 16 caracteres" /> | |
</body> | |
</html> |
This file contains hidden or 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
/* | |
* | |
* Gabriel Cesar | |
* gabrielcesar2 [at] gmail.com | |
* | |
* created_at 20080826 | |
* last_update 20140723 | |
* | |
* Exemplo de uso: token ( 16 ) | |
* Retorno: 142y96c876C2933b | |
* | |
*/ | |
function token ( length ) | |
{ | |
var s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' | |
var hash = '' | |
for ( var i = 0; i < length + 1; i ++ ) | |
{ | |
var rand = Math.ceil ( ( s.length -1 ) * Math.random ()) | |
hash += s [ rand ] | |
} | |
return hash | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment