Software /
code /
verse
Annotate
util/random.lua @ 389:bf3a4fcdcb76
libs.hashes: Wrap LuaCrypto with fallback to util.sha1
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 25 Aug 2015 15:45:23 +0200 |
parent | 388:d963c8a5d89c |
child | 401:7be4ebefd1f4 |
rev | line source |
---|---|
388
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- Prosody IM |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2014 Matthew Wild |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2014 Waqas Hussain |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 -- COPYING file in the source package for more information. |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 -- |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local tostring = tostring; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local os_time = os.time; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local os_clock = os.clock; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local ceil = math.ceil; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local H = require "util.hashes".sha1; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local last_uniq_time = 0; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local function uniq_time() |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local new_uniq_time = os_time(); |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 if last_uniq_time >= new_uniq_time then new_uniq_time = last_uniq_time + 1; end |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 last_uniq_time = new_uniq_time; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 return new_uniq_time; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 end |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local function new_random(x) |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 return H(x..os_clock()..tostring({})); |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 end |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 local buffer = new_random(uniq_time()); |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 local function seed(x) |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 buffer = new_random(buffer..x); |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 end |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 local function bytes(n) |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 if #buffer < n+4 then seed(uniq_time()); end |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 local r = buffer:sub(1, n); |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 buffer = buffer:sub(n+1); |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 return r; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 end |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 return { |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 seed = seed; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 bytes = bytes; |
d963c8a5d89c
Import util.random from Prosody (using SHA-1)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 }; |