Last active
August 29, 2015 14:24
-
-
Save maxidr/42a80103956d2e8a7f14 to your computer and use it in GitHub Desktop.
Random hash in hexa: In nodejs (randomHash.js)
In browser (randomHashBrowser.js)
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
var crypto = require('crypto'); | |
crypto.randomBytes(16).toString('hex'); | |
// 'e834ba2133229fb78a693f5ca3ae05de' |
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
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