Software /
code /
prosody
Annotate
util/random.lua @ 6813:b1615fa62e59
net/server_select: addclient: Check for failure correctly; remove wrapconnection call on failure
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Wed, 18 Dec 2013 17:54:31 -0500 |
parent | 6421:c3011ab945b8 |
child | 7050:ae044691de0f |
rev | line source |
---|---|
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; |
6420 | 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) |
6420 | 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) |
6421
c3011ab945b8
util.random: Ensure at least 4 bytes of previous random state is used when generating new randomness
Kim Alvefur <zash@zash.se>
parents:
6420
diff
changeset
|
34 if #buffer < n+4 then seed(uniq_time()); end |
c3011ab945b8
util.random: Ensure at least 4 bytes of previous random state is used when generating new randomness
Kim Alvefur <zash@zash.se>
parents:
6420
diff
changeset
|
35 local r = buffer:sub(1, n); |
6376
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 }; |