Created
July 28, 2019 04:33
-
-
Save kbjr/23632196cd7ea4818e9f12c3b209d5a2 to your computer and use it in GitHub Desktop.
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 { randomBytes } from 'crypto'; | |
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; | |
const randomString = (length) => { | |
return new Promise((resolve, reject) => { | |
const chars = new Array(length); | |
randomBytes(length, (error, bytes) => { | |
if (error) { | |
return reject(error); | |
} | |
for (let i = 0; i < length; i++) { | |
chars[i] = charset[bytes[i] % charset.length]; | |
} | |
resolve(chars.join('')); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use: