Git Product home page Git Product logo

nanoid's Introduction

Nano ID

Very small and secure a URL-friendly unique ID generator for JavaScript.

var nanoid = require('nanoid')
model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"

Safe. It uses cryptographically strong random APIs and guarantees a proper distribution of symbols.

Small. Only 258 bytes (minified and gzipped). No dependencies.

Compact. It uses more symbols than UUID (A-Za-z0-9_~) and has the same number of unique options in just 22 symbols instead of 36.

The generator supports Node.js and all browsers starting from IE 11.

Sponsored by Evil Martians

Security

See a good article about random generators theory: Secure random values (in Node.js)

Unpredictability

Instead of unsafe Math.random() Nano ID uses crypto module in Node.js and Web Crypto API in browsers.

Uniformity

random % alphabet is a popular mistake to make when coding an ID generator. The spread will not be even; there will be a lower chance for some symbols to appear compared to others—so it will reduce the number of tries when brute-forcing.

Nano ID uses a better algorithm and tests uniformity:

Nano ID uniformity

Usage

Normal

The main module uses URL-friendly symbols (A-Za-z0-9_~) and returns an ID with 22 characters (to have the same uniqueness as UUID v4).

var nanoid = require('nanoid')
model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"

Symbols -,.() are not encoded in URL, but in the end of a link they could be identified as a punctuation symbol.

Custom Alphabet or Length

If you want to change the ID alphabet or the length you can use low-level generate module.

var generate = require('nanoid/generate')
model.id = generate('1234567890abcdef', 10) //=> "4f90d13a42"

If you want to use the same URL-friendly symbols and just change the length, you can get default alphabet from the url module:

var url = require('nanoid/url')
model.id = generate(url, 10) //=> "Uakgb_J5m9"

Custom Random Bytes Generator

You can replace the default safe random generator using the format module. For instance, to use seed-based generator.

var format = require('nanoid/format')

function random (size) {
  var result = []
  for (var i = 0; i < size; i++) result.push(randomByte())
  return result
}

format(random, "abcdef", 10) //=> "fbaefaadeb"

random callback must accept the array size and return an array with random numbers.

nanoid's People

Contributors

ai avatar dshster avatar kossnocorp avatar yaroslav avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.