Random string generator with promise interface for nodejs, browsers, and react-native
MIT License
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
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.
$ npm install randomstring-promise
import randomstringPromise from 'randomstring-promise'; // NodeJS
// import randomstringPromise from 'randomstring-promise/browser'; // Browsers
// import randomstringPromise from 'randomstring-promise/react-native'; // React Native
randomstringPromise()
.then(function(result) {
console.log(result); // u8KNs7aAw0DCOKO1MdEgVIcF2asajrdd
});
randomstringPromise(length, charset)
[0-9a-zA-Z]
[a-zA-Z]
[0-9]
[0-9a-f]
abcxyz
)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
});
React Native implementation depends on a react-native-randombytes's native code so it must be installed (Follow its instruction). But this library does not use its original JavaScript implementation and omits its synchronous function which has more wide and deeper dependencies such as react-native-crypto.