Skip to content

Instantly share code, notes, and snippets.

@maxidr
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxidr/42a80103956d2e8a7f14 to your computer and use it in GitHub Desktop.
Save maxidr/42a80103956d2e8a7f14 to your computer and use it in GitHub Desktop.
Random hash in hexa: In nodejs (randomHash.js) In browser (randomHashBrowser.js)
var crypto = require('crypto');
crypto.randomBytes(16).toString('hex');
// 'e834ba2133229fb78a693f5ca3ae05de'
function randomString(length, chars) {
var result = '';
for(var i = length; i > 0; --i){
result += chars[Math.round(Math.random() * (chars.length - 1))];
}
return result;
}
function randomId(size){
return randomString(size || 32 , '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment