Comparison

util/id.lua @ 8016:9546c629289b

util.id: New util for producing random identifiers of varying sizes
author Kim Alvefur <zash@zash.se>
date Wed, 29 Mar 2017 22:38:31 +0200
child 12110:b5b799a2a10c
comparison
equal deleted inserted replaced
8014:ff3787033abb 8016:9546c629289b
1 -- Prosody IM
2 -- Copyright (C) 2008-2017 Matthew Wild
3 -- Copyright (C) 2008-2017 Waqas Hussain
4 -- Copyright (C) 2008-2017 Kim Alvefur
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10 local s_gsub = string.gsub;
11 local random_bytes = require "util.random".bytes;
12 local base64_encode = require "util.encodings".base64.encode;
13
14 local b64url = { ["+"] = "-", ["/"] = "_", ["="] = "" };
15 local function b64url_random(len)
16 return (s_gsub(base64_encode(random_bytes(len)), "[+/=]", b64url));
17 end
18
19 return {
20 short = function () return b64url_random(6); end;
21 medium = function () return b64url_random(12); end;
22 long = function () return b64url_random(24); end;
23 custom = function (size)
24 return function () return b64url_random(size); end;
25 end;
26 }