util/id.lua

changeset 0
550f506de75a
equal deleted inserted replaced
-1:000000000000 0:550f506de75a
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 }

mercurial