Coder Social home page Coder Social logo

railsmechanic / nanoid Goto Github PK

View Code? Open in Web Editor NEW
210.0 5.0 12.0 68 KB

Elixir port of NanoID, a secure and URL-friendly unique ID generator. https://hex.pm/packages/nanoid

License: MIT License

Elixir 100.00%
idgenerator identifier token elixir-port

nanoid's Introduction

Nanoid port for Elixir Build Status

Elixir port of NanoID (https://github.com/ai/nanoid), a tiny, secure URL-friendly unique string ID generator.

Safe. It uses cryptographically strong random APIs and tests distribution of symbols.

Compact. It uses a larger alphabet than UUID (A-Za-z0-9_-). So ID size was reduced from 36 to 21 symbols.

Installation

The package can be installed as Hex package:

  1. Add nanoid to your list of dependencies in mix.exs:
def deps do
  [{:nanoid, "~> 2.1.0"}]
end
  1. Run mix deps.get to fetch the package from hex

Introducing a new generator

With version 2.0.0 of nanoid, @ai introduces a new non-secure way of creating NanoIDs. In order to keep this port close to the original, this possibility was also introduced in this port. To ensure a certain level of security, nanoid uses per default the secure token generator. But according to your preferences, if you don't need "cryptographically strong random tokens", just use the non-secure token generator.

Configuration

Starting with version 2.0.0 of nanoid for Elixir it's possible to use config.exs to configure nanoid defaults e.g. for different environments.

config :nanoid,
  size: 21,
  alphabet: "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

After changing configuration, remember to re-compile nanoid with following command:

$ mix deps.compile nanoid --force

Why re-compile? Continuously reading the configuration via Application.get_env/3 has a significant effect on the speed of nanoid. Compiling the default configuration into byte-code will eliminate the effect.

Usage

Using the "secure" (default) generator

Generate secure NanoIDs of custom size by using the default alphabet

Generate a secure NanoID with the default size of 21 characters.

iex> Nanoid.generate()
"mJUHrGXZBZpNX50x2xkzf"

Generate a secure NanoID with a custom size of 64 characters.

iex> Nanoid.generate(64)
"wk9fsUrhK9k~MxY0hLazRKpcSlic8XYDFusks7Jb8FwCVnoQaKFSPsmmLHzP7qCX"

Generate secure NanoIDs of custom size by using a custom alphabet

Generate a secure NanoID with the default size of 21 characters and an individual alphabet.

iex> Nanoid.generate(21, "abcdef123")
"d1dcd2dee333cae1bfdea"

Generate a secure NanoID with custom size of 64 characters and an individual alphabet.

iex> Nanoid.generate(64, "abcdef123")
"aabbaca3c11accca213babed2bcd1213efb3e3fa1ad23ecbf11c2ffc123f3bbe"

Using the "non-secure" generator

Generate non-secure NanoIDs of custom size by using the default alphabet

Generate a non-secure NanoID with the default size of 21 characters.

iex> Nanoid.generate_non_secure()
"YBctoD1RuZqv0DLfzDxl2"

Generate a non-secure NanoID with a custom size of 64 characters.

iex> Nanoid.generate_non_secure(64)
"D2WBHGWQOVds4YKuErmOGJ-oYfp5rik5Z-qo7kN1Dw3gv_1qQs6POmhqZdabkf8s"

Generate non-secure NanoIDs of custom size and with a custom alphabet

Generate a non-secure NanoID with the default size of 21 characters and an individual alphabet.

iex> Nanoid.generate_non_secure(21, "abcdef123")
"b12c2fac2bdbcdfcfb2da"

Generate a non-secure NanoID with custom size of 64 characters and an individual alphabet.

iex> Nanoid.generate_non_secure(64, "abcdef123")
"dfc1ed3ea22bed1c3c2df2eb21bbd33efdfae3abd3ca2abcca1efcfbf31a3b3f"

License

The MIT License (MIT). Please see License File for more information.

nanoid's People

Contributors

ai avatar alecho avatar c4710n avatar frm avatar maratgaliev avatar nietaki avatar railsmechanic 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

nanoid's Issues

Compilation warnings with more recent versions of elixir (`Application.get_env/3 is discouraged`, `use Bitwise is deprecated`)

Thank you for this neat package, it's super-useful.

I have been seeing warnings while compiling my applications that use nanoid, using a recent version of Elixir (this is running on Elixir 1.15.0 / OTP 26.0.1, but I have seen this with earlier versions as well):

$ mix compile
...
==> nanoid
Compiling 4 files (.ex)
warning: Application.get_env/3 is discouraged in the module body, use Application.compile_env/3 instead
Warning:   lib/nanoid/configuration.ex:8: Nanoid.Configuration (module)

warning: Application.get_env/3 is discouraged in the module body, use Application.compile_env/3 instead
Warning:   lib/nanoid/configuration.ex:9: Nanoid.Configuration (module)

warning: use Bitwise is deprecated. import Bitwise instead
Warning:   lib/nanoid/secure.ex:6: Nanoid.Secure (module)

Generated nanoid app
...

Thank you!

generate with custom size fails

Hello, looks like Nanoid.generate(size) doesn't generate an id with the desired length consistently.
You can easily verify it with the following script

Enum.map(1..100, fn _x -> String.length(Nanoid.generate(30)) end) |> Enum.uniq()
[30, 29, 28]

Does not respect defaults set in config.exs

It seems that the Nanoid generate functions are not picking up the values set in config.exs.

I currently have this is my config/config.exs inside a phoenix project

config :nanoid,
  size: 21,
  alphabet: "0123456789abcdef"

When I open the shell iex -S mix I get the following

iex(1)> Application.get_env(:nanoid, :size)
21
iex(2)> Application.get_env(:nanoid, :alphabet)
"0123456789abcdef"
iex(3)> Nanoid.Secure.generate()
"yXR2LbmKTzohOefJ5ocCh"
iex(4)> Nanoid.NonSecure.generate()
"obxlccY4icVuAQO2l5vy_"

This shows that the correct configuration is loaded into the iex but it is not being picked up correctly by the module.

API 2.0

Let’s update the library to be compatible with the latest Nano ID 2.0:

  • Replace ~ to - in default alphabet
  • Add non-secure fast generator
  • Async API?

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.