Giter VIP home page Giter VIP logo

cuid's Introduction

CUID

CUID generator for Lua.

Travis Coveralls github LuaRocks license PRs Welcome

For more information, see: http://usecuid.org

Installation

If available on LuaRocks:

$ luarocks --local install cuid

Otherwise, you could install through this root project directory:

$ luarocks --local make

Usage

To load this library, just type:

local cuid = require ("cuid")

Once loaded, you can generate fresh CUIDs through:

local id = cuid.generate ( )

As an example of CUID, we have c00p6qup20000ckkzslahp5pn, where:

  • c is the CUID prefix (so it's a valid variable).
  • 00p6qup2 is a timestamp/continuous number.
  • 0000 is the internal sequential counter.
  • ckkz is the machine/host fingerprint.
  • slahp5pn are pseudo-random numbers.

To generate slugs (shorter and collision-weak CUIDs), just type the following:

local slug = cuid.slug ( )

Slugs are made of 8 characters, and there's no prefix (as is the case of CUIDs - "c" in the case). So, slugs are often used as suffixes, for instance, on URLs.

Configuration

You could as well set a custom fingerprint for CUID generation by an environment variable. This environment variable is called LUA_CUID_FINGERPRINT.

Whenever the cuid library is loaded, it will lookup such environment variable to generate a fingerprint if it is indeed defined.

It's also possible to set a custom fingerprint prior CUID/slug generation, just type the following for both cases:

local cuid = require 'cuid'

local fingerprint_input = "It's me, Mario!"

local id   = cuid.generate (fingerprint_input)
local slug = cuid.slug (fingerprint_input)

Such fingerprint text is used only once per function call, that is, on the next function call, everything remains the same of previous state prior custom fingerprint's function call.

Remarks

Pull requests and issues are welcome! Happy hacking!

cuid's People

Contributors

marcoonroad avatar

Stargazers

 avatar

Watchers

 avatar  avatar

cuid's Issues

Fingerprint environment variable allows arbitrary non-hexadecimal fingerprints.

The problem lies around the following piece of code:

	if FINGERPRINT then
		return pad (FINGERPRINT, BLOCK_SIZE)
	end
...

Here, FINGERPRINT might contain invalid characters (that is, characters out of the hexadecimal value range).

The solution is to catch such thing on tests. Our test must validate if the CUID fields contain valid hexadecimal characters.

๐Ÿ˜ƒ

Discrepancy between base conversions

The library contains the maximum number representable under base-36 by 4 digits (i.e, 1679616 = 36 ** 4), but our internal machinery only converts to base-16 by 4 digits values (the maximum representable value here is 65536 = 16 ** 4).

The following pieces of code contain such problems, the former regarding base-36:

local DISCRETE_VALUES = 1679616 -- 36 ^ BLOCK_SIZE --

And this latter regarding base-16:

local function to_hex (number)
	return string.format ("%x", math.floor (number))
end

We need to extend the range of conversion to match the range of representation, therefore, we need
a new function called to_base36 instead of to_hex here.

We're not obliged to stick to base-36 following CUID specification, but I want to comply how much possible with that.

Cuid standard is deprecated.

Due to security concerns (which also exist in database auto-increment ids, uuid/guid, and most other id standrads), the Cuid standard is now deprecated in favor of Cuid2.

Slugs & API-level custom fingerprints.

The next release (planned to be v0.4-1) will bring the following features:

  • API-level custom fingerprints (rather than just sole environment variables).
  • Slugs (shorter CUIDs) for URL disambiguation, for instance.

The planned API-level fingerprint customization is the following:

local cuid = require 'cuid'
local fingerprint_text = "It's me, Mario!"
local id = cuid.generate (fingerprint_text)

Where the cuid.generate accepts fingerprint_text as an optional argument.
So, it could be called as well:

local id = cuid.generate ( )

And thus, no prior/previous API is broken (in the case, v0.3-1). This default case for CUID
generation will try to figure out a fingerprint out of the LUA_CUID_FINGERPRINT
environment variable, if defined. Otherwise, there's an internal machinery (a.k.a black magic)
which final users (library clients) must not rely upon.

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.