Annotate

util/random.lua @ 6408:7e69d61a0ef7

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 11 Sep 2014 01:17:56 +0200
parent 6376:bd812a7713ad
child 6420:0c070e30a7db
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
bd812a7713ad util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local sha1 = require "util.hashes".sha1;
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)
bd812a7713ad util.random: Generic util lib for generating strings of random bytes
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return sha1(x..os_clock()..tostring({}));
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 };