Browse Source

move all hashing helper code in separate library

next
Georg Hopp 12 years ago
parent
commit
527b9e2d38
  1. 18
      include/application/application.h
  2. 4
      include/auth/auth.h
  3. 9
      include/auth/interface/auth.h
  4. 6
      include/user.h
  5. 33
      include/utils/hash.h
  6. 72
      include/uuid.h
  7. 12
      src/Makefile.am
  8. 8
      src/application/application.c
  9. 13
      src/application/controller/_get_user_from_args.c
  10. 5
      src/application/controller/_update_user_from_args.c
  11. 5
      src/application/controller/_validate_email.c
  12. 8
      src/application/controller/signup/create.c
  13. 8
      src/application/controller/user/create.c
  14. 6
      src/application/controller/user/read.c
  15. 8
      src/application/controller/user/update.c
  16. 15
      src/application/create_user.c
  17. 7
      src/application/get_user.c
  18. 12
      src/application/login.c
  19. 8
      src/application/update_password.c
  20. 13
      src/application/update_user.c
  21. 6
      src/asset/asset.c
  22. 6
      src/auth/auth.c
  23. 3
      src/auth/create.c
  24. 3
      src/auth/interface/auth.c
  25. 6
      src/auth/ldap.c
  26. 7
      src/auth/storage/storage.c
  27. 7
      src/config/value.c
  28. 5
      src/hash/delete.c
  29. 5
      src/hash/get.c
  30. 3
      src/hash/get_first.c
  31. 7
      src/hash/value.c
  32. 7
      src/http/cookie.c
  33. 7
      src/http/header.c
  34. 5
      src/http/writer/write.c
  35. 7
      src/session/session.c
  36. 3
      src/storage/storage.c
  37. 12
      src/taskrambler.c
  38. 15
      src/user/user.c
  39. 55
      src/utils/hash.c
  40. 12
      src/uuid/Makefile.am
  41. 54
      src/uuid/_format3or5.c
  42. 34
      src/uuid/compare.c
  43. 38
      src/uuid/parse.c
  44. 34
      src/uuid/unparse.c
  45. 60
      src/uuid/uuid.c
  46. 38
      src/uuid/version1.c
  47. 71
      src/uuid/version3.c
  48. 71
      src/uuid/version5.c

18
include/application/application.h

@ -25,7 +25,8 @@
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "session.h" #include "session.h"
#include "hash.h" #include "hash.h"
@ -34,7 +35,6 @@
#include "storage/storage.h" #include "storage/storage.h"
#include "session.h" #include "session.h"
#include "user.h" #include "user.h"
#include "uuid.h"
struct randval { struct randval {
@ -54,7 +54,7 @@ TR_CLASS(Application) {
Storage passwords; Storage passwords;
Storage roles; Storage roles;
Uuid user_namespace;
TR_Uuid user_namespace;
Hash roles_user_index; Hash roles_user_index;
Hash roles_resource_index; Hash roles_resource_index;
@ -63,12 +63,12 @@ TR_CLASS(Application) {
const char * loc; const char * loc;
}; };
int applicationLogin(Application, Credential, Session);
void applicationLogout(Application, Session);
Uuid applicationCreateUser(Application, Credential, User);
Uuid applicationUpdateUser(Application, User);
User applicationGetUser(Application, Uuid);
int applicationUpdatePassword(Application, Credential, User);
int applicationLogin(Application, Credential, Session);
void applicationLogout(Application, Session);
TR_Uuid applicationCreateUser(Application, Credential, User);
TR_Uuid applicationUpdateUser(Application, User);
User applicationGetUser(Application, TR_Uuid);
int applicationUpdatePassword(Application, Credential, User);
Session applicationSessionStart(Application); Session applicationSessionStart(Application);
Session applicationSessionGet(Application, const char *); Session applicationSessionGet(Application, const char *);

4
include/auth/auth.h

@ -42,8 +42,8 @@
#ifndef __AUTH_AUTH_H__ #ifndef __AUTH_AUTH_H__
#define __AUTH_AUTH_H__ #define __AUTH_AUTH_H__
#include "trbase.h"
#include "uuid.h"
#include <trbase.h>
#include "auth.h" #include "auth.h"
#include "auth/credential.h" #include "auth/credential.h"

9
include/auth/interface/auth.h

@ -28,18 +28,19 @@
#ifndef __AUTH_INTERFACE_AUTH_H__ #ifndef __AUTH_INTERFACE_AUTH_H__
#define __AUTH_INTERFACE_AUTH_H__ #define __AUTH_INTERFACE_AUTH_H__
#include "trbase.h"
#include "uuid.h"
#include <trbase.h>
#include <trhash.h>
#include "auth/credential.h" #include "auth/credential.h"
typedef int (* fptr_authenticate)(void *, Credential, Uuid);
typedef int (* fptr_authenticate)(void *, Credential, TR_Uuid);
TR_INTERFACE(Auth) { TR_INTERFACE(Auth) {
TR_IFID; TR_IFID;
fptr_authenticate authenticate; fptr_authenticate authenticate;
}; };
extern int authenticate(void *, Credential, Uuid);
extern int authenticate(void *, Credential, TR_Uuid);
#endif // __AUTH_INTERFACE_AUTH_H__ #endif // __AUTH_INTERFACE_AUTH_H__

6
include/user.h

@ -25,14 +25,16 @@
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include "uuid.h"
#include <trbase.h>
#include <trhash.h>
#include "auth.h" #include "auth.h"
TR_CLASS(User) { TR_CLASS(User) {
unsigned long hash; unsigned long hash;
AuthModule auth_type; AuthModule auth_type;
TR_Uuid namespace;
/** /**
* username holds the identifier of the user. * username holds the identifier of the user.
* For ldap users this is the common name. * For ldap users this is the common name.

33
include/utils/hash.h

@ -1,33 +0,0 @@
/**
* \file
* Functions to handle varios signals send to the application.
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2012 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/>.
*/
#ifndef __UTILS_HASH_H__
#define __UTILS_HASH_H__
#include <sys/types.h>
unsigned long sdbm(const unsigned char *, size_t);
#endif // __UTILS_HASH_H__
// vim: set ts=4 sw=4:

72
include/uuid.h

@ -1,72 +0,0 @@
/**
* \file
* ways to create uuid variant 5. For uuid of variant 1 I use
* the implementation delivered with the core utils.
* But this is wrapped in here, so that the rest of the code
* can use only this implementation...this additionally has the
* advantage that we can implement version 1 here too for systems
* where the coreutils implementation is not available.
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2012 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/>.
*/
#ifndef __UUID_H__
#define __UUID_H__
#include <stdint.h>
#include <sys/types.h>
#include <uuid/uuid.h>
#include "trbase.h"
typedef char UuidString[37];
TR_CLASS(Uuid) {
union {
uuid_t value;
struct {
uint32_t time_low;
uint16_t time_mid;
uint16_t time_hi_version;
uint8_t clk_seq_hi_res;
uint8_t clk_seq_low;
unsigned char node[6];
} elements;
} uuid;
};
extern Uuid uuidZero;
/*
* generator functions...these are not really part of the object
* but generate a uuid object.
*/
Uuid uuidVersion1();
Uuid uuidVersion3(const unsigned char *, size_t, Uuid);
Uuid uuidVersion5(const unsigned char *, size_t, Uuid);
void uuidUnparse(Uuid, UuidString);
Uuid uuidParse(const UuidString);
int uuidCompare(Uuid, Uuid);
#endif // __UUID_H__
// vim: set ts=4 sw=4:

12
src/Makefile.am

@ -2,8 +2,7 @@ ACLOCAL_AMFLAGS = -I m4
AUTOMAKE_OPTIONS = subdir-objects AUTOMAKE_OPTIONS = subdir-objects
TRUTILS = utils/hash.c \
utils/http.c \
TRUTILS = utils/http.c \
utils/daemonize.c \ utils/daemonize.c \
utils/signalHandling.c \ utils/signalHandling.c \
utils/mime_type.c utils/mime_type.c
@ -13,8 +12,7 @@ TRCOMMONLIBS = cbuf/libcbuf.la \
queue/libqueue.la \ queue/libqueue.la \
logger/liblogger.la \ logger/liblogger.la \
tree/libtree.la \ tree/libtree.la \
stream/libstream.la \
uuid/libuuid.la
stream/libstream.la
TRHTTPSERVER = http/libhttp.la \ TRHTTPSERVER = http/libhttp.la \
asset/libasset.la \ asset/libasset.la \
@ -29,8 +27,8 @@ TR = ./application/.libs/libapplication.a \
./config/.libs/libconfig.a \ ./config/.libs/libconfig.a \
./router/.libs/librouter.a ./router/.libs/librouter.a
TRLIBS = -ltrutils -ltrhttpserver -ltrcommon
USEDLIBS = -lrt -lssl -lcrypto -lldap -lgdbm -luuid -ldl
TRLIBS = -ltrbase -ltrhashing -ltrutils -ltrhttpserver -ltrcommon
USEDLIBS = -lrt -lssl -lcrypto -lldap -lgdbm -luuid
AM_CFLAGS += -I../include/ AM_CFLAGS += -I../include/
@ -57,4 +55,4 @@ taskrambler_LDFLAGS = -Wl,--export-dynamic \
SUBDIRS = asset auth cbuf hash queue http \ SUBDIRS = asset auth cbuf hash queue http \
logger server session socket stream tree application \ logger server session socket stream tree application \
storage user config router uuid
storage user config router

8
src/application/application.c

@ -24,9 +24,9 @@
#include <stdarg.h> #include <stdarg.h>
#include "trbase.h"
#include "hash.h"
#include "uuid.h"
#include <trbase.h>
#include <trhash.h>
#include "application/application.h" #include "application/application.h"
#include "storage/storage.h" #include "storage/storage.h"
@ -52,7 +52,7 @@ applicationCtor(void * _this, va_list * params)
this->passwords = va_arg(*params, Storage); this->passwords = va_arg(*params, Storage);
//this->roles = va_arg(*params, Storage); //this->roles = va_arg(*params, Storage);
this->user_namespace = uuidParse(va_arg(*params, char *));
this->user_namespace = va_arg(*params, TR_Uuid);
this->auth = va_arg(*params, void *); this->auth = va_arg(*params, void *);

13
src/application/controller/_get_user_from_args.c

@ -22,7 +22,9 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "hash.h" #include "hash.h"
#include "user.h" #include "user.h"
@ -42,7 +44,16 @@ _controllerGetUserFromArgs(Hash args)
return FALSE; return FALSE;
} }
/**
* \todo how should we inject the user namespace here...
* or better, how to handle it at all...
* at least this is true for every controller...
* Additionally, this and the other controller functions
* that create a user in this way will leak memory.
* !!!IMPORTANT!!!
*/
return TR_new(User, return TR_new(User,
TR_uuidParse("14de9e60-d497-4754-be72-f3bed52541fc"),
(char *)(email->value), email->nvalue, (char *)(email->value), email->nvalue,
(char *)(email->value), email->nvalue, (char *)(email->value), email->nvalue,
(char *)(firstname->value), firstname->nvalue, (char *)(firstname->value), firstname->nvalue,

5
src/application/controller/_update_user_from_args.c

@ -22,7 +22,9 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "hash.h" #include "hash.h"
#include "user.h" #include "user.h"
@ -44,6 +46,7 @@ _controllerUpdateUserFromArgs(Hash args, User * user)
} }
new_user = TR_new(User, new_user = TR_new(User,
TR_uuidParse("14de9e60-d497-4754-be72-f3bed52541fc"),
(char *)((*user)->username), *(*user)->nusername, (char *)((*user)->username), *(*user)->nusername,
(char *)(email->value), email->nvalue, (char *)(email->value), email->nvalue,
(char *)(firstname->value), firstname->nvalue, (char *)(firstname->value), firstname->nvalue,

5
src/application/controller/_validate_email.c

@ -22,7 +22,9 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "application/application.h" #include "application/application.h"
#include "session.h" #include "session.h"
#include "hash.h" #include "hash.h"
@ -50,6 +52,7 @@ _controllerCreateUserFromArgs(Hash args)
} }
return TR_new(User, return TR_new(User,
TR_uuidParse("14de9e60-d497-4754-be72-f3bed52541fc"),
(char *)(email->value), email->nvalue, (char *)(email->value), email->nvalue,
(char *)(firstname->value), firstname->nvalue, (char *)(firstname->value), firstname->nvalue,
(char *)(surname->value), surname->nvalue); (char *)(surname->value), surname->nvalue);

8
src/application/controller/signup/create.c

@ -22,7 +22,9 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "application/application.h" #include "application/application.h"
#include "session.h" #include "session.h"
#include "hash.h" #include "hash.h"
@ -42,13 +44,13 @@ controllerSignupCreate(
{ {
Credential credential; Credential credential;
User user; User user;
Uuid user_id;
TR_Uuid user_id;
char * response_data; char * response_data;
_controllerProcessUserCreateArgs(args, &user, &credential); _controllerProcessUserCreateArgs(args, &user, &credential);
user_id = applicationCreateUser(application, credential, user); user_id = applicationCreateUser(application, credential, user);
if (0 == uuidCompare(uuidZero, user_id)) {
if (0 == TR_uuidCompare(TR_uuidZero, user_id)) {
response_data = NULL; response_data = NULL;
} else { } else {
applicationLogin(application, credential, session); applicationLogin(application, credential, session);

8
src/application/controller/user/create.c

@ -22,7 +22,9 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "application/application.h" #include "application/application.h"
#include "session.h" #include "session.h"
#include "hash.h" #include "hash.h"
@ -41,13 +43,13 @@ controllerUserCreate(
{ {
Credential credential; Credential credential;
User user; User user;
Uuid user_id;
TR_Uuid user_id;
char * response_data; char * response_data;
_controllerProcessUserCreateArgs(args, &user, &credential); _controllerProcessUserCreateArgs(args, &user, &credential);
user_id = applicationCreateUser(application, credential, user); user_id = applicationCreateUser(application, credential, user);
if (0 == uuidCompare(uuidZero, user_id)) {
if (0 == TR_uuidCompare(TR_uuidZero, user_id)) {
response_data = NULL; response_data = NULL;
} else { } else {
response_data = controllerCurrentuserRead(application, session, NULL); response_data = controllerCurrentuserRead(application, session, NULL);

6
src/application/controller/user/read.c

@ -25,7 +25,9 @@
#include <sys/types.h> #include <sys/types.h>
#include <stdio.h> #include <stdio.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "application/application.h" #include "application/application.h"
#include "session.h" #include "session.h"
#include "hash.h" #include "hash.h"
@ -40,7 +42,7 @@ controllerUserRead(Application app, Session sess, Hash args)
char * buffer; char * buffer;
size_t nbuffer; size_t nbuffer;
HashValue id = hashGet(args, CSTRA("id")); HashValue id = hashGet(args, CSTRA("id"));
Uuid search = uuidParse(id->value);
TR_Uuid search = TR_uuidParse(id->value);
User user = applicationGetUser(app, search); User user = applicationGetUser(app, search);
nbuffer = snprintf(NULL, 0, USER_JSON, nbuffer = snprintf(NULL, 0, USER_JSON,

8
src/application/controller/user/update.c

@ -22,7 +22,9 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "application/application.h" #include "application/application.h"
#include "session.h" #include "session.h"
#include "hash.h" #include "hash.h"
@ -38,14 +40,14 @@ controllerUserUpdate(
Session session, Session session,
Hash args) Hash args)
{ {
Uuid user_id;
TR_Uuid user_id;
if (! _controllerUpdateUserFromArgs(args, &(session->user))) { if (! _controllerUpdateUserFromArgs(args, &(session->user))) {
return NULL; return NULL;
} }
user_id = applicationUpdateUser(application, session->user); user_id = applicationUpdateUser(application, session->user);
if (0 == uuidCompare(uuidZero, user_id)) {
if (0 == TR_uuidCompare(TR_uuidZero, user_id)) {
return NULL; return NULL;
} }
TR_delete(user_id); TR_delete(user_id);

15
src/application/create_user.c

@ -26,14 +26,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "auth.h" #include "auth.h"
#include "user.h" #include "user.h"
#include "uuid.h"
#include "storage/storage.h" #include "storage/storage.h"
#include "application/application.h" #include "application/application.h"
Uuid
TR_Uuid
applicationCreateUser( applicationCreateUser(
Application this, Application this,
Credential cred, Credential cred,
@ -41,9 +42,9 @@ applicationCreateUser(
{ {
char * user_serialized; char * user_serialized;
size_t nuser_serialized; size_t nuser_serialized;
Uuid index;
TR_Uuid index;
index = TR_indexUuid(user, this->user_namespace);
index = TR_getIndex(user);
TR_serialize(user, (unsigned char **)&user_serialized, &nuser_serialized); TR_serialize(user, (unsigned char **)&user_serialized, &nuser_serialized);
if (SPR_OK != storagePut( if (SPR_OK != storagePut(
@ -53,7 +54,7 @@ applicationCreateUser(
user_serialized, user_serialized,
nuser_serialized)) nuser_serialized))
{ {
return uuidZero;
return TR_uuidZero;
} }
TR_MEM_FREE(user_serialized); TR_MEM_FREE(user_serialized);
@ -68,7 +69,7 @@ applicationCreateUser(
(char *)(index->uuid).value, (char *)(index->uuid).value,
sizeof((index->uuid).value)); sizeof((index->uuid).value));
return uuidZero;
return TR_uuidZero;
} }
return index; return index;

7
src/application/get_user.c

@ -24,15 +24,16 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "auth.h" #include "auth.h"
#include "user.h" #include "user.h"
#include "uuid.h"
#include "storage/storage.h" #include "storage/storage.h"
#include "application/application.h" #include "application/application.h"
User User
applicationGetUser(Application this, Uuid uuid)
applicationGetUser(Application this, TR_Uuid uuid)
{ {
char * user_serialized; char * user_serialized;
size_t nuser_serialized; size_t nuser_serialized;

12
src/application/login.c

@ -26,9 +26,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "auth.h" #include "auth.h"
#include "uuid.h"
#include "storage/storage.h" #include "storage/storage.h"
#include "application/application.h" #include "application/application.h"
@ -39,14 +40,14 @@ applicationLogin(
Credential credential, Credential credential,
Session session) Session session)
{ {
Uuid search;
TR_Uuid search;
AuthModule auth_module; AuthModule auth_module;
User user = TR_new(User, NULL);
User user = TR_new(User, this->user_namespace, NULL);
user->username = CRED_PWD(credential).user; user->username = CRED_PWD(credential).user;
user->nusername = &CRED_PWD(credential).nuser; user->nusername = &CRED_PWD(credential).nuser;
search = TR_indexUuid(user, this->user_namespace);
search = TR_getIndex(user);
auth_module = authenticate(this->auth, credential, search); auth_module = authenticate(this->auth, credential, search);
@ -84,6 +85,7 @@ applicationLogin(
session->user->username = NULL; session->user->username = NULL;
TR_delete(session->user); TR_delete(session->user);
session->user = TR_new(User, session->user = TR_new(User,
this->user_namespace,
CRED_PWD(credential).user, CRED_PWD(credential).user,
CRED_PWD(credential).nuser, CRED_PWD(credential).nuser,
CSTRA(""), CSTRA(""),

8
src/application/update_password.c

@ -26,7 +26,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "auth.h" #include "auth.h"
#include "user.h" #include "user.h"
#include "storage/storage.h" #include "storage/storage.h"
@ -42,7 +44,7 @@ applicationUpdatePassword(
unsigned char hash_data[SALT_SIZE+HASH_SIZE]; unsigned char hash_data[SALT_SIZE+HASH_SIZE];
unsigned char * salt = NULL; unsigned char * salt = NULL;
unsigned char * hash = hash_data+SALT_SIZE; unsigned char * hash = hash_data+SALT_SIZE;
Uuid index;
TR_Uuid index;
if (FALSE == hash_pw( if (FALSE == hash_pw(
CRED_PWD(cred).pass, CRED_PWD(cred).pass,
@ -55,7 +57,7 @@ applicationUpdatePassword(
memcpy(hash_data, salt, SALT_SIZE); memcpy(hash_data, salt, SALT_SIZE);
TR_MEM_FREE(salt); TR_MEM_FREE(salt);
index = TR_indexUuid(user, this->user_namespace);
index = TR_getIndex(user);
storageUpdate( storageUpdate(
this->passwords, this->passwords,
(char *)(index->uuid).value, (char *)(index->uuid).value,

13
src/application/update_user.c

@ -26,24 +26,25 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "auth.h" #include "auth.h"
#include "user.h" #include "user.h"
#include "uuid.h"
#include "storage/storage.h" #include "storage/storage.h"
#include "application/application.h" #include "application/application.h"
Uuid
TR_Uuid
applicationUpdateUser( applicationUpdateUser(
Application this, Application this,
User user) User user)
{ {
char * user_serialized; char * user_serialized;
size_t nuser_serialized; size_t nuser_serialized;
Uuid index;
TR_Uuid index;
index = TR_indexUuid(user, this->user_namespace);
index = TR_getIndex(user);
TR_serialize(user, (unsigned char **)&user_serialized, &nuser_serialized); TR_serialize(user, (unsigned char **)&user_serialized, &nuser_serialized);
if (SPR_OK != storageUpdate( if (SPR_OK != storageUpdate(
@ -53,7 +54,7 @@ applicationUpdateUser(
user_serialized, user_serialized,
nuser_serialized)) nuser_serialized))
{ {
return uuidZero;
return TR_uuidZero;
} }
TR_MEM_FREE(user_serialized); TR_MEM_FREE(user_serialized);

6
src/asset/asset.c

@ -36,13 +36,13 @@
// for localtime // for localtime
#include <time.h> #include <time.h>
#include <trbase.h>
#include <trhash.h>
#include "trbase.h"
#include "asset.h" #include "asset.h"
#include "hash.h" #include "hash.h"
#include "utils/mime_type.h" #include "utils/mime_type.h"
#include "utils/hash.h"
#include "utils/http.h" #include "utils/http.h"
@ -62,7 +62,7 @@ assetCtor(void * _this, va_list * params)
strncpy(this->fname, fname, 2048); strncpy(this->fname, fname, 2048);
this->fname[2048] = '\0'; this->fname[2048] = '\0';
this->hash = sdbm(
this->hash = TR_sdbm(
(unsigned char *)this->fname, (unsigned char *)this->fname,
this->nfname); this->nfname);

6
src/auth/auth.c

@ -26,8 +26,8 @@
#include <stdio.h> #include <stdio.h>
#include <ldap.h> #include <ldap.h>
#include "trbase.h"
#include "uuid.h"
#include <trbase.h>
#include <trhash.h>
#include "auth.h" #include "auth.h"
#include "auth/credential.h" #include "auth/credential.h"
@ -61,7 +61,7 @@ authDtor(void * _this)
static static
int int
authAuthenticate(void * _this, Credential cred, Uuid user_index)
authAuthenticate(void * _this, Credential cred, TR_Uuid user_index)
{ {
Auth this = _this; Auth this = _this;
int i; int i;

3
src/auth/create.c

@ -23,7 +23,8 @@
#include <stdarg.h> #include <stdarg.h>
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include <trbase.h>
#include "auth.h" #include "auth.h"
#include "auth/ldap.h" #include "auth/ldap.h"
#include "auth/storage.h" #include "auth/storage.h"

3
src/auth/interface/auth.c

@ -20,7 +20,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "uuid.h"
#include "auth/auth.h" #include "auth/auth.h"
#include "auth/credential.h" #include "auth/credential.h"
#include "auth/interface/auth.h" #include "auth/interface/auth.h"
@ -28,7 +27,7 @@
TR_CREATE_INTERFACE(Auth, 1); TR_CREATE_INTERFACE(Auth, 1);
int int
authenticate(void * auth, Credential cred, Uuid user_index)
authenticate(void * auth, Credential cred, TR_Uuid user_index)
{ {
int ret; int ret;

6
src/auth/ldap.c

@ -26,8 +26,8 @@
#include <stdio.h> #include <stdio.h>
#include <ldap.h> #include <ldap.h>
#include "trbase.h"
#include "uuid.h"
#include <trbase.h>
#include <trhash.h>
#include "auth/ldap.h" #include "auth/ldap.h"
#include "auth/credential.h" #include "auth/credential.h"
@ -68,7 +68,7 @@ authLdapDtor(void * _this)
static static
int int
authLdapAuthenticate(void * _this, Credential cred, Uuid user_index)
authLdapAuthenticate(void * _this, Credential cred, TR_Uuid user_index)
{ {
AuthLdap this = _this; AuthLdap this = _this;
char who[256]; char who[256];

7
src/auth/storage/storage.c

@ -20,10 +20,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "storage/storage.h" #include "storage/storage.h"
#include "auth.h" #include "auth.h"
#include "uuid.h"
#include "user.h" #include "user.h"
static static
@ -45,7 +46,7 @@ authStorageDtor(void * _this)
static static
int int
authStorageAuthenticate(void * _this, Credential cred, Uuid user_index)
authStorageAuthenticate(void * _this, Credential cred, TR_Uuid user_index)
{ {
AuthStorage this = _this; AuthStorage this = _this;

7
src/config/value.c

@ -25,9 +25,10 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "config/value.h" #include "config/value.h"
#include "utils/hash.h"
static static
int int
@ -40,7 +41,7 @@ configValueCtor(void * _this, va_list * params)
char * value = va_arg(*params, char *); char * value = va_arg(*params, char *);
size_t nvalue = va_arg(*params, size_t); size_t nvalue = va_arg(*params, size_t);
this->hash = sdbm((unsigned char *)key, nkey);
this->hash = TR_sdbm((unsigned char *)key, nkey);
/** /**
* if we find the value enclosed by single or double * if we find the value enclosed by single or double

5
src/hash/delete.c

@ -22,9 +22,10 @@
#include <sys/types.h> #include <sys/types.h>
#include <trhash.h>
#include "asset.h" #include "asset.h"
#include "hash.h" #include "hash.h"
#include "utils/hash.h"
static static
inline inline
@ -47,7 +48,7 @@ hashDeleteComp(const void * a, const void * b)
void * void *
hashDelete(Hash this, const char * search, size_t nsearch) hashDelete(Hash this, const char * search, size_t nsearch)
{ {
unsigned long hash = sdbm((const unsigned char *)search, nsearch);
unsigned long hash = TR_sdbm((const unsigned char *)search, nsearch);
void * found = NULL; void * found = NULL;
found = treeDelete(&(this->root), &hash, hashDeleteComp); found = treeDelete(&(this->root), &hash, hashDeleteComp);

5
src/hash/get.c

@ -25,9 +25,10 @@
#include <search.h> #include <search.h>
#include <sys/types.h> #include <sys/types.h>
#include <trhash.h>
#include "hash.h" #include "hash.h"
#include "tree.h" #include "tree.h"
#include "utils/hash.h"
static static
inline inline
@ -50,7 +51,7 @@ hashGetComp(const void * a, const void * b)
void * void *
hashGet(Hash this, const char * search, size_t nsearch) hashGet(Hash this, const char * search, size_t nsearch)
{ {
unsigned long hash = sdbm((const unsigned char *)search, nsearch);
unsigned long hash = TR_sdbm((const unsigned char *)search, nsearch);
void * found = treeFind(this->root, &hash, hashGetComp); void * found = treeFind(this->root, &hash, hashGetComp);
return found; return found;

3
src/hash/get_first.c

@ -25,9 +25,10 @@
#include <search.h> #include <search.h>
#include <sys/types.h> #include <sys/types.h>
#include <trhash.h>
#include "hash.h" #include "hash.h"
#include "tree.h" #include "tree.h"
#include "utils/hash.h"
void * void *
hashGetFirst(Hash this) hashGetFirst(Hash this)

7
src/hash/value.c

@ -25,8 +25,9 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include "utils/hash.h"
#include <trbase.h>
#include <trhash.h>
#include "hash/value.h" #include "hash/value.h"
#include "hash/interface/hashable.h" #include "hash/interface/hashable.h"
@ -46,7 +47,7 @@ hashValueCtor(void * _this, va_list * params)
this->key[this->nkey] = 0; this->key[this->nkey] = 0;
memcpy(this->key, key, this->nkey); memcpy(this->key, key, this->nkey);
this->hash = sdbm((unsigned char *)this->key, this->nkey);
this->hash = TR_sdbm((unsigned char *)this->key, this->nkey);
if (NULL != value) { if (NULL != value) {
this->value = TR_malloc(this->nvalue + 1); this->value = TR_malloc(this->nvalue + 1);

7
src/http/cookie.c

@ -25,10 +25,11 @@
#include <stdarg.h> #include <stdarg.h>
#include <sys/types.h> #include <sys/types.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "hash.h" #include "hash.h"
#include "http/cookie.h" #include "http/cookie.h"
#include "utils/hash.h"
static static
@ -51,7 +52,7 @@ httpCookieCtor(void * _this, va_list * params)
this->value[this->nvalue] = 0; this->value[this->nvalue] = 0;
memcpy(this->value, value, this->nvalue); memcpy(this->value, value, this->nvalue);
this->hash = sdbm((unsigned char *)key, nkey);
this->hash = TR_sdbm((unsigned char *)key, nkey);
return 0; return 0;
} }

7
src/http/header.c

@ -24,10 +24,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "hash.h" #include "hash.h"
#include "http/header.h" #include "http/header.h"
#include "utils/hash.h"
static static
int int
@ -45,7 +46,7 @@ httpHeaderCtor(void * _this, va_list * params) {
this->name[this->nname] = 0; this->name[this->nname] = 0;
memcpy(this->name, name, this->nname); memcpy(this->name, name, this->nname);
this->hash = sdbm((unsigned char *)name, this->nname);
this->hash = TR_sdbm((unsigned char *)name, this->nname);
(this->value)[0] = TR_malloc((this->nvalue)[0] + 1); (this->value)[0] = TR_malloc((this->nvalue)[0] + 1);
(this->value)[0][(this->nvalue)[0]] = 0; (this->value)[0][(this->nvalue)[0]] = 0;

5
src/http/writer/write.c

@ -23,7 +23,8 @@
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "trbase.h"
#include <trbase.h>
#include "http/message.h" #include "http/message.h"
#include "queue.h" #include "queue.h"
#include "http/writer.h" #include "http/writer.h"
@ -48,7 +49,7 @@ httpWriterWrite(void * _this, Stream st)
this->written = 0; this->written = 0;
this->nheader = httpMessageHeaderSizeGet(this->current); this->nheader = httpMessageHeaderSizeGet(this->current);
if (this->nheader > TR_memGetSize(this->buffer)) {
if (this->nheader > TR_getSize(this->buffer)) {
ssize_t size = this->nheader; ssize_t size = this->nheader;
size = (0 != size%WRITER_BUF_CHUNK)? size = (0 != size%WRITER_BUF_CHUNK)?

7
src/session/session.c

@ -28,10 +28,11 @@
#include <sys/types.h> #include <sys/types.h>
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "session.h" #include "session.h"
#include "hash.h" #include "hash.h"
#include "utils/hash.h"
static static
@ -45,7 +46,7 @@ sessionCtor(void * _this, va_list * params)
uuid_generate(uuid); uuid_generate(uuid);
uuid_unparse(uuid, this->id); uuid_unparse(uuid, this->id);
this->hash = sdbm((unsigned char *)this->id, 36);
this->hash = TR_sdbm((unsigned char *)this->id, 36);
return 0; return 0;
} }

3
src/storage/storage.c

@ -24,7 +24,8 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "trbase.h"
#include <trbase.h>
#include "storage/storage.h" #include "storage/storage.h"

12
src/taskrambler.c

@ -36,6 +36,9 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <errno.h> #include <errno.h>
#include <trbase.h>
#include <trhash.h>
#include "server.h" #include "server.h"
#include "logger.h" #include "logger.h"
#include "http/worker.h" #include "http/worker.h"
@ -45,7 +48,6 @@
#include "config/config.h" #include "config/config.h"
#include "config/value.h" #include "config/value.h"
#include "trbase.h"
#include "logger.h" #include "logger.h"
#include "utils/signalHandling.h" #include "utils/signalHandling.h"
@ -164,6 +166,7 @@ main()
ApplicationAdapterHttp adapterHttp; ApplicationAdapterHttp adapterHttp;
HttpWorker worker; HttpWorker worker;
Server server; Server server;
TR_Uuid user_namespace;
ConfigValue ldap_base = ConfigValue ldap_base =
configGet(config, CSTRA("ldap_base")); configGet(config, CSTRA("ldap_base"));
@ -205,12 +208,14 @@ main()
authCreate(auth, AUTH_STORAGE, passwords); authCreate(auth, AUTH_STORAGE, passwords);
user_namespace = TR_uuidParse("14de9e60-d497-4754-be72-f3bed52541fc");
application = TR_new( application = TR_new(
Application, Application,
value, value,
users, users,
passwords, passwords,
"14de9e60-d497-4754-be72-f3bed52541fc",
user_namespace,
auth); auth);
router = TR_new(Router, application); router = TR_new(Router, application);
@ -272,6 +277,7 @@ main()
TR_delete(adapterHttp); TR_delete(adapterHttp);
TR_delete(router); TR_delete(router);
TR_delete(application); TR_delete(application);
TR_delete(user_namespace);
TR_delete(passwords); TR_delete(passwords);
TR_delete(users); TR_delete(users);
TR_delete(auth); TR_delete(auth);
@ -285,7 +291,7 @@ main()
TR_delete(config); TR_delete(config);
TR_delete(logger); TR_delete(logger);
TR_memCleanup();
TR_cleanup();
return 0; return 0;
} }

15
src/user/user.c

@ -20,16 +20,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "user.h"
#include "uuid.h"
#include "trbase.h"
#include <trbase.h>
#include <trhash.h>
#include "user.h"
static static
int int
userCtor(void * _this, va_list * params) userCtor(void * _this, va_list * params)
{ {
User this = _this; User this = _this;
this->namespace = va_arg(* params, TR_Uuid);
char * username = va_arg(* params, char *); char * username = va_arg(* params, char *);
if (NULL != username) { if (NULL != username) {
@ -140,15 +142,14 @@ userUnserialize(
static static
void * void *
userIndexUuid(void * _this, void * _namespace)
userIndexUuid(void * _this)
{ {
User this = _this; User this = _this;
Uuid namespace = _namespace;
return uuidVersion3(
return TR_uuidVersion3(
(unsigned char *)this->username, (unsigned char *)this->username,
*this->nusername, *this->nusername,
namespace);
this->namespace);
} }

55
src/utils/hash.c

@ -1,55 +0,0 @@
/**
* \file
*
* \author Georg Hopp
* \author Unknown (find out)
*
* \copyright
* Copyright © 2012 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 <ctype.h>
#include <sys/types.h>
#include <openssl/rand.h>
#include "utils/hash.h"
/**
* SDBM hashing algorithm:
*
* this algorithm was created for sdbm (a public-domain reimplementation of
* ndbm) database library. it was found to do well in scrambling bits,
* causing better distribution of the keys and fewer splits. it also happens
* to be a good general hashing function with good distribution. the actual
* function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below
* is the faster version used in gawk. [there is even a faster, duff-device
* version] the magic constant 65599 was picked out of thin air while
* experimenting with different constants, and turns out to be a prime. this
* is one of the algorithms used in berkeley db (see sleepycat) and elsewhere.
*/
unsigned long
sdbm(const unsigned char * str, size_t len)
{
unsigned long hash = 0;
for(; 0 < len; str++, len--)
hash = tolower(*str) + (hash << 6) + (hash << 16) - hash;
return hash;
}
// vim: set ts=4 sw=4:

12
src/uuid/Makefile.am

@ -1,12 +0,0 @@
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)

54
src/uuid/_format3or5.c

@ -1,54 +0,0 @@
/**
* \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 "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:

34
src/uuid/compare.c

@ -1,34 +0,0 @@
/**
* \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 "uuid.h"
int
uuidCompare(Uuid uuid1, Uuid uuid2)
{
return uuid_compare((uuid1->uuid).value, (uuid2->uuid).value);
}
// vim: set ts=4 sw=4:

38
src/uuid/parse.c

@ -1,38 +0,0 @@
/**
* \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 "trbase.h"
#include "uuid.h"
Uuid
uuidParse(const UuidString uuid_str)
{
Uuid uuid = TR_new(Uuid);
uuid_parse(uuid_str, (uuid->uuid).value);
return uuid;
}
// vim: set ts=4 sw=4:

34
src/uuid/unparse.c

@ -1,34 +0,0 @@
/**
* \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 "uuid.h"
void
uuidUnparse(Uuid uuid, UuidString uuid_str)
{
uuid_unparse((uuid->uuid).value, uuid_str);
}
// vim: set ts=4 sw=4:

60
src/uuid/uuid.c

@ -1,60 +0,0 @@
/**
* \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 "trbase.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);
}
TR_INIT_IFACE(TR_Class, uuidCtor, uuidDtor, uuidClone);
TR_CREATE_CLASS(Uuid, NULL, TR_IF(TR_Class));
TR_INSTANCE(Uuid, uuidZero) {
.uuid.value = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
}};
// vim: set ts=4 sw=4:

38
src/uuid/version1.c

@ -1,38 +0,0 @@
/**
* \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 "trbase.h"
#include "uuid.h"
Uuid
uuidVersion1()
{
Uuid uuid = TR_new(Uuid);
uuid_generate((uuid->uuid).value);
return uuid;
}
// vim: set ts=4 sw=4:

71
src/uuid/version3.c

@ -1,71 +0,0 @@
/**
* \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 "trbase.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 = TR_clone(nsid);
Uuid uuid = TR_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);
TR_delete(net_nsid);
_uuidFormat3or5(uuid, hash, 3);
return uuid;
}
// vim: set ts=4 sw=4:

71
src/uuid/version5.c

@ -1,71 +0,0 @@
/**
* \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 "trbase.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 = TR_clone(nsid);
Uuid uuid = TR_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);
TR_delete(net_nsid);
_uuidFormat3or5(uuid, hash, 5);
return uuid;
}
// vim: set ts=4 sw=4:
Loading…
Cancel
Save