Created
February 11, 2018 15:22
-
-
Save matinkaboli/1d7213f6968e18f0a5b805a665e4d079 to your computer and use it in GitHub Desktop.
Random String in Node.js (only with built-it packages)
This file contains 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
import crypto from 'crypto'; | |
function randomString() { | |
return new Promise((resolve, reject) => { | |
crypto.pseudoRandomBytes(16, (err, raw) => { | |
if (err) { | |
reject(err); | |
} else { | |
const r = raw.toString('hex') + Date.now(); | |
resolve(r); | |
} | |
}); | |
}); | |
} | |
export default randomString; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment