From 604931c6291c2616ab712a12cd1cc4eecbaef920 Mon Sep 17 00:00:00 2001 From: bossiel Date: Mon, 8 Feb 2010 22:22:20 +0000 Subject: [PATCH] Add support for Universally Unique Identifier (UUID) version 5 as per RFC 4122. --- trunk/tinySAK/src/tsk.h | 1 + trunk/tinySAK/src/tsk_string.c | 2 +- trunk/tinySAK/src/tsk_uuid.c | 80 ++++++++++++++++++++++++++++++++++ trunk/tinySAK/src/tsk_uuid.h | 48 ++++++++++++++++++++ trunk/tinySAK/test/test.c | 12 ++++- trunk/tinySAK/test/test.vcproj | 4 ++ trunk/tinySAK/test/test_uuid.h | 35 +++++++++++++++ trunk/tinySAK/tinySAK.vcproj | 8 ++++ 8 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 trunk/tinySAK/src/tsk_uuid.c create mode 100644 trunk/tinySAK/src/tsk_uuid.h create mode 100644 trunk/tinySAK/test/test_uuid.h diff --git a/trunk/tinySAK/src/tsk.h b/trunk/tinySAK/src/tsk.h index 0d846eef..415d5c4e 100644 --- a/trunk/tinySAK/src/tsk.h +++ b/trunk/tinySAK/src/tsk.h @@ -62,6 +62,7 @@ TSK_BEGIN_DECLS #include "tsk_md5.h" #include "tsk_hmac.h" #include "tsk_base64.h" +#include "tsk_uuid.h" TSK_END_DECLS diff --git a/trunk/tinySAK/src/tsk_string.c b/trunk/tinySAK/src/tsk_string.c index fa9a5450..7659b2f5 100644 --- a/trunk/tinySAK/src/tsk_string.c +++ b/trunk/tinySAK/src/tsk_string.c @@ -57,7 +57,7 @@ /**@page tsk_string_page String utils Tutorial */ -static char HEX[] = "0123456789ABCDEF"; +static char HEX[] = "0123456789abcdef"; /**@ingroup tsk_string_group * From base 10 to base 16 diff --git a/trunk/tinySAK/src/tsk_uuid.c b/trunk/tinySAK/src/tsk_uuid.c new file mode 100644 index 00000000..28f585f0 --- /dev/null +++ b/trunk/tinySAK/src/tsk_uuid.c @@ -0,0 +1,80 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO 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. +* +* DOUBANGO 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 Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ + +/**@file tsk_uuid.c + * @brief Universally Unique Identifier (UUID version 5) implementation (RFC 4122). + * This implementation is not fully conform to RFC 4122 but could be safely used to generate random UUIDs. + * + * @author Mamadou Diop + * + * @date Created: Sat Nov 8 16:54:58 2009 mdiop + */ +#include "tsk_uuid.h" + +#include "tsk_sha1.h" +#include "tsk_string.h" +#include "tsk_time.h" + +#include + +int tsk_uuidgenerate(tsk_uuidstring_t *result) +{ + /* From wikipedia + * Version 5 UUIDs use a scheme with SHA-1 hashing, otherwise it is the same idea as in version 3. + * RFC 4122 states that version 5 is preferred over version 3 name based UUIDs. + * Note that the 160 bit SHA-1 hash is truncated to 128 bits to make the length work out. + */ + tsk_sha1string_t sha1result; + tsk_istr_t epoch; + unsigned i, k; + static char HEX[] = "0123456789abcdef"; + + tsk_itoa(tsk_time_epoch(), &epoch); + tsk_sha1compute(epoch, sizeof(epoch), &sha1result); + + /* XOR the SHA-1 result with random numbers. */ + for(i=0; i<(TSK_UUID_DIGEST_SIZE*2); i+=4){ + *((uint32_t*)&sha1result[i]) ^= rand(); + + for(k=0; k +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO 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. +* +* DOUBANGO 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 Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ + +/**@file tsk_uuid.h + * @brief Universally Unique Identifier (UUID version 5) implementation (RFC 4122). + * This implementation is not fully conform to RFC 4122 but could be safely used to generate random UUIDs. + * + * @author Mamadou Diop + * + * @date Created: Sat Nov 8 16:54:58 2009 mdiop + */ +#ifndef _TINYSAK_UUID_H_ +#define _TINYSAK_UUID_H_ + +#include "tinySAK_config.h" + +TSK_BEGIN_DECLS + +#define TSK_UUID_DIGEST_SIZE 16 +#define TSK_UUID_STRING_SIZE ((TSK_UUID_DIGEST_SIZE*2)+4/*-*/) + +typedef char tsk_uuidstring_t[TSK_UUID_STRING_SIZE+1]; /**< Hexadecimal UUID digest string. */ +typedef char tsk_uuiddigest_t[TSK_UUID_DIGEST_SIZE]; /**< UUID digest bytes. */ + +TINYSAK_API int tsk_uuidgenerate(tsk_uuidstring_t *result); + +TSK_END_DECLS + +#endif /* _TINYSAK_UUID_H_ */ diff --git a/trunk/tinySAK/test/test.c b/trunk/tinySAK/test/test.c index 0f927db8..4c17619d 100644 --- a/trunk/tinySAK/test/test.c +++ b/trunk/tinySAK/test/test.c @@ -46,10 +46,11 @@ #define RUN_TEST_PARAMS 0 #define RUN_TEST_TIMER 0 #define RUN_TEST_RUNNABLE 0 -#define RUN_TEST_BUFFER 1 +#define RUN_TEST_BUFFER 0 #define RUN_TEST_MD5 0 #define RUN_TEST_SHA1 0 #define RUN_TEST_BASE64 0 +#define RUN_TEST_UUID 1 #if RUN_TEST_LISTS || RUN_TEST_ALL #include "test_lists.h" @@ -115,6 +116,9 @@ #include "test_base64.h" #endif +#if RUN_TEST_UUID || RUN_TEST_ALL +#include "test_uuid.h" +#endif @@ -229,6 +233,12 @@ int main() /* test base64 encoding/decoding */ test_base64(); #endif + +#if RUN_TEST_UUID || RUN_TEST_ALL + /* test fake UUID (version5) */ + test_uuid(); +#endif + } getchar(); diff --git a/trunk/tinySAK/test/test.vcproj b/trunk/tinySAK/test/test.vcproj index 0666e7a9..26a4df58 100644 --- a/trunk/tinySAK/test/test.vcproj +++ b/trunk/tinySAK/test/test.vcproj @@ -431,6 +431,10 @@ RelativePath=".\test_url.h" > + + +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO 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. +* +* DOUBANGO 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 Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef _TEST_UUID_H_ +#define _TEST_UUID_H_ + +void test_uuid() +{ + tsk_uuidstring_t uuid; + + tsk_uuidgenerate(&uuid); + + TSK_DEBUG_INFO("Generated UUID: %s", uuid); +} + +#endif /* _TEST_UUID_H_ */ + diff --git a/trunk/tinySAK/tinySAK.vcproj b/trunk/tinySAK/tinySAK.vcproj index 1b01b5cc..96011722 100644 --- a/trunk/tinySAK/tinySAK.vcproj +++ b/trunk/tinySAK/tinySAK.vcproj @@ -458,6 +458,10 @@ RelativePath=".\src\tsk_url.h" > + + @@ -568,6 +572,10 @@ RelativePath=".\src\tsk_url.c" > + +