Giter Site home page Giter Site logo

uid-promise's Introduction

uid-promise

Creates a cryptographically secure UID with a 62 character range that can be safely used in URLs.

Usage

Install the package:

npm i uid-promise

Then import it:

import { uid } = from 'uid-promise';

Finally, call it:

await uid(20);

API

uid(Number len) => Promise

  • Return a Promise that resolves with a string of random characters of length len
  • len must always be provided, else the promise is rejected
  • Under the hood, crypto.randomBytes is used
  • Character set: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

Authors

uid-promise's People

Contributors

agadzik avatar greenkeeperio-bot avatar jfmengels avatar leerob avatar leo avatar lucleray avatar mrmckeb avatar rauchg avatar rickycodes avatar stripedpajamas avatar styfle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

uid-promise's Issues

Length of UIDCHARS makes uid's biased

The length of UIDCHARS is 62, which doesn't divide 256, the amount of possibilities of a byte from crypto.randomBytes. Therefore, the first 8 characters ("ABCDEFGH") will be chosen somewhat more often than the rest.
The rest of the characters each have 4/256 ~= 1.56% chance to be chosen, while the first 8 have 5/256 ~= 1.95% chance โ€“ an extra 0.39%.
I don't know exactly how this should compromise security, but I doubt the generated UID's can be rightfully called "cryptographically secure", since bias in a random generator is always bad.

There are two ways to deal with this:

  • Add two characters to UIDCHARS (for example, - and _, since they too are unreserved in URI's according to the RFC), to make its length 64, which divides 256;
  • Reject values >=248 from crypto.randomBytes. This effectively reduces its range to 248 choices, which 62 divides. When using this, one can't simply request the required amount of bytes from crypto.randomBytes and be done with it; a reasonable strategy would probably be to request some more bytes, making sure that in the overwhelming majority of cases, there are still enough bytes to cover the UID. In the case that there are not enough, you'd need to ask for more crypto bytes.

You can fix this or not fix this. The current code is probably fine for what you're using it for, but if you leave it like it is, I suggest changing the wording of the readme.

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.