lua-resty-uuid

May 7, 2026 · View on GitHub

LuaJIT FFI bindings for libuuid — a DCE-compatible Universally Unique Identifier (UUID/GUID) library. Generates v1 (time), v4 (random), v6 (reordered time) and v7 (Unix-epoch) UUIDs, plus parses and inspects existing UUID strings.

Status

Production-ready. Used in OpenResty / LuaJIT environments. Pure FFI bindings — no C build step required, just libuuid on the system.

Requirements

  • LuaJIT (or Lua 5.1+ with the luaffi shim)
  • libuuid available on the dynamic loader path
    • Debian / Ubuntu: apt install uuid-dev (provides libuuid.so)
    • Alpine: apk add util-linux-dev
    • RHEL / Fedora: dnf install libuuid-devel
    • macOS: bundled with the system (loaded from libSystem)

Installation

Via LuaRocks

luarocks install lua-resty-uuid

From source

git clone https://github.com/bungle/lua-resty-uuid
cd lua-resty-uuid
luarocks make

The library will look for libuuid first in package.cpath, then on the system loader path, falling back to symbols already present in the host process.

Quick start

local uuid = require "resty.uuid"

uuid.generate()         --> "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"
uuid.generate_random()  --> "550e8400-e29b-41d4-a716-446655440000"  (v4)
uuid.generate_time()    --> "..."                                   (v1)
uuid.generate_time_v6() --> "..."                                   (v6)
uuid.generate_time_v7() --> "..."                                   (v7)

uuid()                  --> shortcut for uuid.generate()

uuid.is_valid("not-a-uuid")  --> false
uuid.type(some_v4_id)        --> 4
uuid.variant(some_id)        --> 1   (DCE)
uuid.time(some_v1_id)        --> 1714867200, 123456

API

The module is a table that is also callable. Calling uuid() is a shortcut for uuid.generate().

See also LDocs.

Generators

FunctionReturnsDescription
uuid.generate()stringBest generator available on the host (random when possible, otherwise time-based).
uuid.generate_random()stringVersion 4 (random) UUID.
uuid.generate_time()stringVersion 1 (timestamp + node id) UUID.
uuid.generate_time_safe()string, booleanVersion 1 UUID and a flag indicating whether generation was synchronized for uniqueness.
uuid.generate_time_v6()stringVersion 6 UUID — v1 fields reordered for lexical sortability.
uuid.generate_time_v7()stringVersion 7 UUID — millisecond Unix timestamp + random tail.

All generators return the canonical 36-character 8-4-4-4-12 lower-case hex form, e.g. "f81d4fae-7dec-11d0-a765-00a0c91e6bf6".

Inspection

FunctionReturnsDescription
uuid.is_valid(id)booleanWhether id parses as a UUID.
uuid.type(id)int or nilUUID version (UUID_TYPE_* constant), or nil if invalid.
uuid.variant(id)int or nilUUID variant (UUID_VARIANT_* constant), or nil if invalid.
uuid.time(id)number, number or nilSeconds since the Unix epoch and microseconds within that second; nil if id is not a valid UUID. Meaningful only for v1, v6 and v7.

Constant reference

uuid.type returns one of:

ValueConstantMeaning
1UUID_TYPE_DCE_TIMETime-based (v1)
2UUID_TYPE_DCE_SECURITYDCE Security (v2)
3UUID_TYPE_DCE_MD5Name-based MD5 (v3)
4UUID_TYPE_DCE_RANDOMRandom (v4)
5UUID_TYPE_DCE_SHA1Name-based SHA-1 (v5)
6UUID_TYPE_DCE_TIME_V6Reordered time (v6)
7UUID_TYPE_DCE_TIME_V7Unix-epoch time (v7)

uuid.variant returns one of:

ValueConstantMeaning
0UUID_VARIANT_NCSNCS / Apollo legacy
1UUID_VARIANT_DCEDCE 1.1 / RFC 4122
2UUID_VARIANT_MICROSOFTMicrosoft GUID
3UUID_VARIANT_OTHERReserved

Testing

make test          # full pipeline: install rock, run busted, coverage, lint
make unit          # busted only
make coverage      # busted + luacov report
make lint          # luacheck

Tests live in spec/ and exercise every public function plus the __call metamethod.

Documentation

API reference is built with LDoc and lives under docs/:

make docs

License

BSD 2-Clause. See LICENSE.

Copyright (c) 2014 — 2026 Aapo Talvensaari.