Random string generator with Promise interface
npm install randomstring-promise
Random string generator inspired by node-randomstring with consistent promise interface for Nodejs, web browsers, and ReactNative.
This library uses following RNGs to generate cryptographically secure random value.
- crypto.randomBytes (NodeJS)
- window.crypto.getRandomValues (Browsers)
- react-native-randombytes (ReactNative)
(react-native-randombytes only provides asynchronous function and that's why this library focuses on asynchronous interface only.)
If you need only web-browser implementation, also check randomstring-browserify.
shell
$ npm install randomstring-promise
`Usage
`javascript
import randomstringPromise from 'randomstring-promise'; // NodeJS
// import randomstringPromise from 'randomstring-promise/browser'; // Browsers
// import randomstringPromise from 'randomstring-promise/react-native'; // React NativerandomstringPromise()
.then(function(result) {
console.log(result); // u8KNs7aAw0DCOKO1MdEgVIcF2asajrdd
});
`Options
randomstringPromise(length, charset)
- length: Integer (default: 32)
- charset: String (default: 'alphanumeric')
* alphanumeric [0-9a-zA-Z]
* alphabetic [a-zA-Z]
* numeric [0-9]
* hex [0-9a-f]
* your own (specify your own character set as a string like abcxyz)`javascript
randomstringPromise(16) // length=16, charset='alphanumeric' (default)
.then(function(result) {
console.log(result); // 4BfHIF0s697EOW9Y
});randomstringPromise(16, 'numeric') // length=16, charset='numeric'
.then(function(result) {
console.log(result); // 3122428966440165
});
randomstringPromise(8, 'abc') // length=8, customized charset 'a', 'b', and 'c'
.then(function(result) {
console.log(result); // ccbcacbc
});
``