crypto-random-string
Generate a cryptographically strong random string
Last updated 4 months ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install crypto-random-string 
SYNC missed versions from official npm registry.

crypto-random-string Build Status

Generate a cryptographically strong random string

Can be useful for creating an identifier, slug, salt, PIN code, fixture, etc.

Install

$ npm install crypto-random-string

Usage

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, type: 'numeric'});
//=> '8314659141'

cryptoRandomString({length: 6, type: 'distinguishable'});
//=> 'CDEHKM'

cryptoRandomString({length: 10, characters: 'abc'});
//=> 'abaaccabac'

API

cryptoRandomString(options)

Returns a randomized string. Hex by default.

options

Type: object

length

Required
Type: number

Length of the returned string.

type

Type: string
Default: 'hex'
Values: 'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable'

Use only characters from a predefined set of allowed characters.

Cannot be set at the same time as the characters option.

The distinguishable set contains only uppercase characters that are not easily confused: CDEHKMPRTUWXY012458. It can be useful if you need to print out a short string that you'd like users to read and type back in with minimal errors. For example, reading a code off of a screen that needs to be typed into a phone to connect two devices.

characters

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.

Related


Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

Current Tags

  • 3.2.0                                ...           latest (4 months ago)

6 Versions

  • 3.2.0                                ...           4 months ago
  • 3.1.0                                ...           6 months ago
  • 3.0.1                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 403
This Week 11,510
This Month 55,281
Last Day 11,107
Last Week 67,133
Last Month 262,127
Dependencies (1)
Dev Dependencies (3)
Dependents (194)

Copyright 2014 - 2016 © taobao.org |