Generate a cryptographically strong random string
Can be useful for creating an identifier, slug, salt, fixture, etc.
$ npm install crypto-random-string
const cryptoRandomString = require('crypto-random-string');
cryptoRandomString({length: 10});
//=> '2cf05d94db'
cryptoRandomString({length: 10, type: 'base64'});
//=> 'YMiMbaQl6I'
cryptoRandomString({length: 10, type: 'url-safe'});
//=> 'YN-tqc8pOw'
cryptoRandomString({length: 10, characters: '1234567890'});
//=> '1791935639'
Returns a randomized string. Hex by default.
Type: object
Required
Type: number
Length of the returned string.
Type: string
Default: 'hex'
Values: 'hex'
'base64'
'url-safe'
Use only characters from a predefined set of allowed characters.
Cannot be set at the same time as the characters
option.
Type: string
Minimum length: 1
Maximum length: 65536
Use only characters from a custom set of allowed characters.
Cannot be set at the same time as the type
option.
- random-int - Generate a random integer
- random-float - Generate a random float
- random-item - Get a random item from an array
- random-boolean - Get a random boolean
- random-obj-key - Get a random key from an object
- random-obj-prop - Get a random property from an object
- unique-random - Generate random numbers that are consecutively unique
MIT © Sindre Sorhus