util/id.lua

Thu, 03 Dec 2020 17:05:27 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 03 Dec 2020 17:05:27 +0000
changeset 0
550f506de75a
permissions
-rw-r--r--

Initial commit

0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2017 Matthew Wild
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2017 Waqas Hussain
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- Copyright (C) 2008-2017 Kim Alvefur
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 --
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 --
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local s_gsub = string.gsub;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local random_bytes = require "util.random".bytes;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local base64_encode = require "util.encodings".base64.encode;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local b64url = { ["+"] = "-", ["/"] = "_", ["="] = "" };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local function b64url_random(len)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 return (s_gsub(base64_encode(random_bytes(len)), "[+/=]", b64url));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 return {
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 short = function () return b64url_random(6); end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 medium = function () return b64url_random(12); end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 long = function () return b64url_random(24); end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 custom = function (size)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return function () return b64url_random(size); end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 }

mercurial