author | Kim Alvefur <zash@zash.se> |
Thu, 18 Sep 2014 10:16:59 +0200 | |
changeset 6423 | 0c070e30a7db |
parent 6376 | bd812a7713ad |
child 6424 | c3011ab945b8 |
permissions | -rw-r--r-- |
6376
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
-- Prosody IM |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
-- Copyright (C) 2008-2014 Matthew Wild |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
-- Copyright (C) 2008-2014 Waqas Hussain |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
-- |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
-- This project is MIT/X11 licensed. Please see the |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
-- COPYING file in the source package for more information. |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
-- |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
|
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
local tostring = tostring; |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
local os_time = os.time; |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
local os_clock = os.clock; |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
local ceil = math.ceil; |
6423 | 13 |
local H = require "util.hashes".sha512; |
6376
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
|
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
local last_uniq_time = 0; |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
local function uniq_time() |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
local new_uniq_time = os_time(); |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
if last_uniq_time >= new_uniq_time then new_uniq_time = last_uniq_time + 1; end |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
last_uniq_time = new_uniq_time; |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
return new_uniq_time; |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 |
end |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
|
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
local function new_random(x) |
6423 | 24 |
return H(x..os_clock()..tostring({})); |
6376
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
end |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
|
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
local buffer = new_random(uniq_time()); |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
|
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
local function seed(x) |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
buffer = new_random(buffer..x); |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
end |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
|
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
local function bytes(n) |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
if #buffer < n then seed(uniq_time()); end |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
local r = buffer:sub(0, n); |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
buffer = buffer:sub(n+1); |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
return r; |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
end |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
|
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
return { |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
seed = seed; |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
bytes = bytes; |
bd812a7713ad
util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
}; |