Browse Source
add code to generate uuid version 3 and 5. With this we can generate the same uuid for a given name all the time
v0.1.8
add code to generate uuid version 3 and 5. With this we can generate the same uuid for a given name all the time
v0.1.8
17 changed files with 575 additions and 27 deletions
-
1configure.ac
-
2include/session.h
-
15include/user.h
-
31include/uuid.h
-
7src/Makefile.am
-
93src/router/route.c
-
44src/testers/uuid.c
-
2src/user/save.c
-
12src/uuid/Makefile.am
-
55src/uuid/_format3or5.c
-
35src/uuid/compare.c
-
38src/uuid/parse.c
-
35src/uuid/unparse.c
-
56src/uuid/uuid.c
-
38src/uuid/version1.c
-
69src/uuid/version3.c
-
69src/uuid/version5.c
@ -1,18 +1,46 @@ |
|||
#include <uuid/uuid.h> |
|||
#include <stdio.h> |
|||
#include <uuid/uuid.h> |
|||
|
|||
#include "class.h" |
|||
#include "uuid.h" |
|||
|
|||
#include "commons.h" |
|||
#include "utils/memory.h" |
|||
|
|||
|
|||
#define UUID_NS "4f947b70-6f9f-43b6-8dc1-1321977c8240" |
|||
|
|||
|
|||
int |
|||
main(int argc, char * argv[]) |
|||
{ |
|||
uuid_t uuid; |
|||
char uuid_str[37]; |
|||
UuidString uuid_str; |
|||
|
|||
Uuid nsid = uuidParse(UUID_NS); |
|||
Uuid ver1 = uuidVersion1(); |
|||
Uuid ver3 = uuidVersion3("foo", 3, nsid); |
|||
Uuid ver5 = uuidVersion3("foo", 5, nsid); |
|||
|
|||
uuidUnparse(nsid, uuid_str); |
|||
printf("nsid: %s\n", uuid_str); |
|||
|
|||
uuidUnparse(ver1, uuid_str); |
|||
printf("nsid: %s\n", uuid_str); |
|||
|
|||
uuidUnparse(ver3, uuid_str); |
|||
printf("nsid: %s\n", uuid_str); |
|||
|
|||
uuidUnparse(ver5, uuid_str); |
|||
printf("nsid: %s\n", uuid_str); |
|||
|
|||
uuid_generate(uuid); |
|||
uuid_unparse(uuid, uuid_str); |
|||
delete(ver5); |
|||
delete(ver3); |
|||
delete(ver1); |
|||
delete(nsid); |
|||
|
|||
printf("%s\n", uuid_str); |
|||
memCleanup(); |
|||
|
|||
return 0; |
|||
return 0; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
// vim: set et ts=4 sw=4: |
|||
@ -0,0 +1,12 @@ |
|||
ACLOCAL_AMFLAGS = -I m4 |
|||
AUTOMAKE_OPTIONS = subdir-objects |
|||
|
|||
UUID = uuid.c version1.c version3.c version5.c _format3or5.c \
|
|||
parse.c unparse.c compare.c |
|||
|
|||
AM_CFLAGS += -I../../include/ |
|||
|
|||
noinst_LTLIBRARIES = libuuid.la |
|||
|
|||
libuuid_la_SOURCES = $(UUID) |
|||
libuuid_la_CFLAGS = $(AM_CFLAGS) |
|||
@ -0,0 +1,55 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2013 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
// for memcpy |
|||
#include <string.h> |
|||
|
|||
// for ntohl and similar |
|||
#include <arpa/inet.h> |
|||
|
|||
// for already available uuid functionality |
|||
#include <uuid/uuid.h> |
|||
|
|||
#include "class.h" |
|||
#include "uuid.h" |
|||
|
|||
void |
|||
_uuidFormat3or5(Uuid uuid, unsigned char hash[16], int version) |
|||
{ |
|||
/* convert UUID to local byte order */ |
|||
memcpy((uuid->uuid).value, hash, 16); |
|||
|
|||
(uuid->uuid).elements.time_low = |
|||
ntohl((uuid->uuid).elements.time_low); |
|||
(uuid->uuid).elements.time_mid = |
|||
ntohs((uuid->uuid).elements.time_mid); |
|||
(uuid->uuid).elements.time_hi_version = |
|||
ntohs((uuid->uuid).elements.time_hi_version); |
|||
|
|||
/* put in the variant and version bits */ |
|||
(uuid->uuid).elements.time_hi_version &= 0x0FFF; |
|||
(uuid->uuid).elements.time_hi_version |= (version << 12); |
|||
(uuid->uuid).elements.clk_seq_hi_res &= 0x3F; |
|||
(uuid->uuid).elements.clk_seq_hi_res |= 0x80; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,35 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2013 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
// for already available uuid functionality |
|||
#include <uuid/uuid.h> |
|||
|
|||
#include "class.h" |
|||
#include "uuid.h" |
|||
|
|||
int |
|||
uuidCompare(Uuid uuid1, Uuid uuid2) |
|||
{ |
|||
return uuid_compare((uuid1->uuid).value, (uuid2->uuid).value); |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,38 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2013 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
// for already available uuid functionality |
|||
#include <uuid/uuid.h> |
|||
|
|||
#include "class.h" |
|||
#include "uuid.h" |
|||
|
|||
Uuid |
|||
uuidParse(const UuidString uuid_str) |
|||
{ |
|||
Uuid uuid = new(Uuid); |
|||
uuid_parse(uuid_str, (uuid->uuid).value); |
|||
|
|||
return uuid; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,35 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2013 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
// for already available uuid functionality |
|||
#include <uuid/uuid.h> |
|||
|
|||
#include "class.h" |
|||
#include "uuid.h" |
|||
|
|||
void |
|||
uuidUnparse(Uuid uuid, UuidString uuid_str) |
|||
{ |
|||
uuid_unparse((uuid->uuid).value, uuid_str); |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,56 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2013 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include <stdarg.h> |
|||
#include <string.h> |
|||
|
|||
#include "class.h" |
|||
#include "uuid.h" |
|||
|
|||
|
|||
static |
|||
int |
|||
uuidCtor(void * _this, va_list * params) |
|||
{ |
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
uuidDtor(void * _this) |
|||
{ |
|||
} |
|||
|
|||
static |
|||
void |
|||
uuidClone(void * _this, void * _base) |
|||
{ |
|||
Uuid this = _this; |
|||
Uuid base = _base; |
|||
|
|||
memcpy((this->uuid).value, (base->uuid).value, 16); |
|||
} |
|||
|
|||
INIT_IFACE(Class, uuidCtor, uuidDtor, uuidClone); |
|||
CREATE_CLASS(Uuid, NULL, IFACE(Class)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,38 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2013 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
// for already available uuid functionality |
|||
#include <uuid/uuid.h> |
|||
|
|||
#include "class.h" |
|||
#include "uuid.h" |
|||
|
|||
Uuid |
|||
uuidVersion1() |
|||
{ |
|||
Uuid uuid = new(Uuid); |
|||
uuid_generate((uuid->uuid).value); |
|||
|
|||
return uuid; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,69 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2013 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
// for size_t |
|||
#include <sys/types.h> |
|||
|
|||
// for md5 generation |
|||
#include <openssl/md5.h> |
|||
|
|||
// for htonl and similar |
|||
#include <arpa/inet.h> |
|||
|
|||
// for already available uuid functionality |
|||
#include "class.h" |
|||
#include "uuid.h" |
|||
|
|||
void _uuidFormat3or5(Uuid uuid, unsigned char hash[16], int version); |
|||
|
|||
Uuid |
|||
uuidVersion3(const unsigned char * name, size_t nname, Uuid nsid) |
|||
{ |
|||
MD5_CTX ctx; |
|||
unsigned char hash[16]; |
|||
Uuid net_nsid = clone(nsid); |
|||
Uuid uuid = new(Uuid); |
|||
|
|||
/* |
|||
* put the namespace id into network byte order. |
|||
*/ |
|||
(net_nsid->uuid).elements.time_low = |
|||
htonl((net_nsid->uuid).elements.time_low); |
|||
(net_nsid->uuid).elements.time_mid = |
|||
htons((net_nsid->uuid).elements.time_mid); |
|||
(net_nsid->uuid).elements.time_hi_version = |
|||
htons((net_nsid->uuid).elements.time_hi_version); |
|||
|
|||
/* |
|||
* generate the MD5 |
|||
*/ |
|||
MD5_Init(&ctx); |
|||
MD5_Update(&ctx, (net_nsid->uuid).value, 16); |
|||
MD5_Update(&ctx, name, nname); |
|||
MD5_Final(hash, &ctx); |
|||
|
|||
_uuidFormat3or5(uuid, hash, 3); |
|||
|
|||
return uuid; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,69 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2013 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
// for size_t |
|||
#include <sys/types.h> |
|||
|
|||
// for sha1 generation |
|||
#include <openssl/sha.h> |
|||
|
|||
// for htonl and similar |
|||
#include <arpa/inet.h> |
|||
|
|||
// for already available uuid functionality |
|||
#include "class.h" |
|||
#include "uuid.h" |
|||
|
|||
void _uuidFormat3or5(Uuid uuid, unsigned char hash[16], int version); |
|||
|
|||
Uuid |
|||
uuidVersion5(const unsigned char * name, size_t nname, Uuid nsid) |
|||
{ |
|||
SHA_CTX ctx; |
|||
unsigned char hash[20]; |
|||
Uuid net_nsid = clone(nsid); |
|||
Uuid uuid = new(Uuid); |
|||
|
|||
/* |
|||
* put the namespace id into network byte order. |
|||
*/ |
|||
(net_nsid->uuid).elements.time_low = |
|||
htonl((net_nsid->uuid).elements.time_low); |
|||
(net_nsid->uuid).elements.time_mid = |
|||
htons((net_nsid->uuid).elements.time_mid); |
|||
(net_nsid->uuid).elements.time_hi_version = |
|||
htons((net_nsid->uuid).elements.time_hi_version); |
|||
|
|||
/* |
|||
* generate the MD5 |
|||
*/ |
|||
SHA1_Init(&ctx); |
|||
SHA1_Update(&ctx, (net_nsid->uuid).value, 16); |
|||
SHA1_Update(&ctx, name, nname); |
|||
SHA1_Final(hash, &ctx); |
|||
|
|||
_uuidFormat3or5(uuid, hash, 5); |
|||
|
|||
return uuid; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue