author | Kim Alvefur <zash@zash.se> |
Wed, 06 Jan 2016 02:46:47 +0100 | |
changeset 7060 | c633e1338554 |
parent 7013 | 001b3cff4ed8 |
child 7064 | eda0feeaf759 |
child 7079 | ad9e683b8f0b |
permissions | -rw-r--r-- |
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1304
diff
changeset
|
1 |
-- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
2 |
-- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
3 |
-- Copyright (C) 2008-2010 Waqas Hussain |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
145
diff
changeset
|
4 |
-- |
758 | 5 |
-- This project is MIT/X11 licensed. Please see the |
6 |
-- COPYING file in the source package for more information. |
|
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
145
diff
changeset
|
7 |
-- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
145
diff
changeset
|
8 |
|
7060 | 9 |
local error = error; |
10 |
local round_up = math.ceil; |
|
11 |
local urandom, urandom_err = io.open("/dev/urandom", "r+"); |
|
1302
4561c6d95339
util.uuid: More uniqueness!
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
12 |
|
44
80d2ade0fd69
Add "uuid" library and make sessionmanager use this.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
module "uuid" |
80d2ade0fd69
Add "uuid" library and make sessionmanager use this.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
|
1303
2170e2c0d57a
util.uuid: Now generates RFC 4122 complaint UUIDs (version 4 - random)
Waqas Hussain <waqas20@gmail.com>
parents:
1302
diff
changeset
|
15 |
local function get_nibbles(n) |
7060 | 16 |
local binary_random = urandom:read(round_up(n/2)); |
17 |
local hex_random = binary_random:gsub(".", |
|
18 |
function (x) return ("%02x"):format(x:byte()) end); |
|
19 |
return hex_random:sub(1, n); |
|
1303
2170e2c0d57a
util.uuid: Now generates RFC 4122 complaint UUIDs (version 4 - random)
Waqas Hussain <waqas20@gmail.com>
parents:
1302
diff
changeset
|
20 |
end |
2170e2c0d57a
util.uuid: Now generates RFC 4122 complaint UUIDs (version 4 - random)
Waqas Hussain <waqas20@gmail.com>
parents:
1302
diff
changeset
|
21 |
local function get_twobits() |
7060 | 22 |
return ("%x"):format(urandom:read(1):byte() % 4 + 8); |
1303
2170e2c0d57a
util.uuid: Now generates RFC 4122 complaint UUIDs (version 4 - random)
Waqas Hussain <waqas20@gmail.com>
parents:
1302
diff
changeset
|
23 |
end |
2170e2c0d57a
util.uuid: Now generates RFC 4122 complaint UUIDs (version 4 - random)
Waqas Hussain <waqas20@gmail.com>
parents:
1302
diff
changeset
|
24 |
|
1302
4561c6d95339
util.uuid: More uniqueness!
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
25 |
function generate() |
7060 | 26 |
if not urandom then |
27 |
error("Unable to obtain a secure random number generator, please see https://prosody.im/doc/random ("..urandom_err..")"); |
|
28 |
end |
|
1303
2170e2c0d57a
util.uuid: Now generates RFC 4122 complaint UUIDs (version 4 - random)
Waqas Hussain <waqas20@gmail.com>
parents:
1302
diff
changeset
|
29 |
-- generate RFC 4122 complaint UUIDs (version 4 - random) |
2170e2c0d57a
util.uuid: Now generates RFC 4122 complaint UUIDs (version 4 - random)
Waqas Hussain <waqas20@gmail.com>
parents:
1302
diff
changeset
|
30 |
return get_nibbles(8).."-"..get_nibbles(4).."-4"..get_nibbles(3).."-"..(get_twobits())..get_nibbles(3).."-"..get_nibbles(12); |
1302
4561c6d95339
util.uuid: More uniqueness!
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
31 |
end |
7060 | 32 |
|
33 |
function seed(x) |
|
34 |
urandom:write(x); |
|
35 |
urandom:flush(); |
|
36 |
end |
|
1302
4561c6d95339
util.uuid: More uniqueness!
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
37 |
|
4561c6d95339
util.uuid: More uniqueness!
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
38 |
return _M; |