Annotate

util/uuid.lua @ 7383:69827ee1f951

MUC: Accept missing form as "instant room" request (fixes #377)
author Kim Alvefur <zash@zash.se>
date Tue, 19 Apr 2016 17:20:39 +0200
parent 7076:ad9e683b8f0b
child 7078:ec17115e3721
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
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
7057
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
9 local error = error;
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
10 local round_up = math.ceil;
7076
ad9e683b8f0b util.uuid: Open /dev/urandom read-only, make seed() a noop
Kim Alvefur <zash@zash.se>
parents: 7057
diff changeset
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)
7057
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
16 local binary_random = urandom:read(round_up(n/2));
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
17 local hex_random = binary_random:gsub(".",
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
18 function (x) return ("%02x"):format(x:byte()) end);
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
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()
7057
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
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()
7057
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
26 if not urandom then
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
27 error("Unable to obtain a secure random number generator, please see https://prosody.im/doc/random ("..urandom_err..")");
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
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
7057
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
32
7076
ad9e683b8f0b util.uuid: Open /dev/urandom read-only, make seed() a noop
Kim Alvefur <zash@zash.se>
parents: 7057
diff changeset
33 function seed()
7057
c633e1338554 util.uuid: Use /dev/urandom
Kim Alvefur <zash@zash.se>
parents: 7010
diff changeset
34 end
1302
4561c6d95339 util.uuid: More uniqueness!
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
35
4561c6d95339 util.uuid: More uniqueness!
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
36 return _M;