Skip to content

Instantly share code, notes, and snippets.

@findzen
Created January 23, 2012 19:47
Show Gist options
  • Select an option

  • Save findzen/1665188 to your computer and use it in GitHub Desktop.

Select an option

Save findzen/1665188 to your computer and use it in GitHub Desktop.
hash generator
var hash = function(s){
var n;
if (typeof(s) == 'number' && s === parseInt(s, 10)){
s = Array(s + 1).join('x');
}
return s.replace(/x/g, function(){
var n = Math.round(Math.random() * 61) + 48;
n = n > 57 ? (n + 7 > 90 ? n + 13 : n + 7) : n;
return String.fromCharCode(n);
});
}
@findzen
Copy link
Author

findzen commented Jan 23, 2012

Function generates random stirng containing characters: 0-9a-zA-Z.
Function takes one argument which may be Integer or String.
If argument is Integer then argument defines length of generated string.
If argument is String then given string is returned but all 'x' characters are replaced with random characters.

hash(5);
"XjJfV"

hash('xxxx-xx-xxxx-2011')
"YDFb-5e-cShX-2011"

hash('ABCD'); // No x letter so nothing to replace
"ABCD"

== Hash based on date - fast way ==

If you want hash based on date you may just use

hash = (+new Date());
console.log(hash);
1317814876299

source: http://mynthon.net/howto/-/webdev/javascript-random-hash-generator.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment