Skip to content

Instantly share code, notes, and snippets.

@gabrielcesar
Last active August 29, 2015 14:04
Show Gist options
  • Save gabrielcesar/59981583ef1251e51bef to your computer and use it in GitHub Desktop.
Save gabrielcesar/59981583ef1251e51bef to your computer and use it in GitHub Desktop.
Generates Tokens in JavaScript.
<!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>
/*
*
* 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