util/random.lua
author Kim Alvefur <zash@zash.se>
Thu, 11 Dec 2014 09:18:39 +0100
changeset 6535 a966efeb6cb3
parent 6424 c3011ab945b8
child 7053 ae044691de0f
permissions -rw-r--r--
mod_storage_sql2, util.sql: Set character encoding on every connect
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
0c070e30a7db util.random: Switch to SHA512
Kim Alvefur <zash@zash.se>
parents: 6376
diff changeset
    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
0c070e30a7db util.random: Switch to SHA512
Kim Alvefur <zash@zash.se>
parents: 6376
diff changeset
    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)
6424
c3011ab945b8 util.random: Ensure at least 4 bytes of previous random state is used when generating new randomness
Kim Alvefur <zash@zash.se>
parents: 6423
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: 6423
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
};