diff options
author | Kim Alvefur <zash@zash.se> | 2017-03-29 22:38:31 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2017-03-29 22:38:31 +0200 |
commit | 656d2ad6c78aa50ee79020c99e20c061506e7426 (patch) | |
tree | 217da4a056d0369933ff5fcdaa59ba678cc4c43f /util/id.lua | |
parent | bcc0a4f11d07b3ff01011790c33abf1b4298c244 (diff) | |
download | prosody-656d2ad6c78aa50ee79020c99e20c061506e7426.tar.gz prosody-656d2ad6c78aa50ee79020c99e20c061506e7426.zip |
util.id: New util for producing random identifiers of varying sizes
Diffstat (limited to 'util/id.lua')
-rw-r--r-- | util/id.lua | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/util/id.lua b/util/id.lua new file mode 100644 index 00000000..731355fa --- /dev/null +++ b/util/id.lua @@ -0,0 +1,26 @@ +-- Prosody IM +-- Copyright (C) 2008-2017 Matthew Wild +-- Copyright (C) 2008-2017 Waqas Hussain +-- Copyright (C) 2008-2017 Kim Alvefur +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + +local s_gsub = string.gsub; +local random_bytes = require "util.random".bytes; +local base64_encode = require "util.encodings".base64.encode; + +local b64url = { ["+"] = "-", ["/"] = "_", ["="] = "" }; +local function b64url_random(len) + return (s_gsub(base64_encode(random_bytes(len)), "[+/=]", b64url)); +end + +return { + short = function () return b64url_random(6); end; + medium = function () return b64url_random(12); end; + long = function () return b64url_random(24); end; + custom = function (size) + return function () return b64url_random(size); end; + end; +} |