aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSaltyMilk <soufiane.elmelcaoui@gmail.com>2020-07-03 21:13:48 +0200
committerSaltyMilk <soufiane.elmelcaoui@gmail.com>2020-07-03 21:13:48 +0200
commitc4dabb4b5079a24b5ef653aa8752cc3cb01fafb2 (patch)
tree27437694d0cebe8a2638eb85036a4586d9a6f1f8
downloadtrand-c4dabb4b5079a24b5ef653aa8752cc3cb01fafb2.tar.gz
trand-c4dabb4b5079a24b5ef653aa8752cc3cb01fafb2.tar.bz2
trand-c4dabb4b5079a24b5ef653aa8752cc3cb01fafb2.zip
initial commit
-rw-r--r--Makefile23
-rw-r--r--libtrand.abin0 -> 2728 bytes
-rw-r--r--readme2
-rw-r--r--trand.c67
-rw-r--r--trand.h16
5 files changed, 108 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..5d7f370
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,23 @@
+NAME= libtrand.a
+CC= gcc
+SRC= trand.c
+OBJ= $(SRC:.c=.o)
+CFLAG= -c -Wextra -Werror
+
+DEF_THREAD=19
+
+all: $(NAME)
+
+$(NAME): $(OBJ)
+ ar -rc $(NAME) $(OBJ)
+
+%.o: %.c
+ $(CC) $(CFLAG) -D DEF_THREAD=$(DEF_THREAD) $?
+
+clean:
+ rm -f $(OBJ)
+fclean:
+ rm -f $(OBJ) $(NAME)
+
+re : fclean all
+
diff --git a/libtrand.a b/libtrand.a
new file mode 100644
index 0000000..70cc0e2
--- /dev/null
+++ b/libtrand.a
Binary files differ
diff --git a/readme b/readme
new file mode 100644
index 0000000..544c8be
--- /dev/null
+++ b/readme
@@ -0,0 +1,2 @@
+Trand is more secure alternative to rand
+To compile: gcc your.c -L path_to_libtrand.a -ltrand
diff --git a/trand.c b/trand.c
new file mode 100644
index 0000000..54cdbb7
--- /dev/null
+++ b/trand.c
@@ -0,0 +1,67 @@
+#include "trand.h"
+
+int g_rfound;
+
+void *thread(void *ma)
+{
+ static pthread_mutex_t mutex;
+ pthread_mutex_t *m = &mutex;
+ int r;
+ for (unsigned long long i = 0; i < *(unsigned long long*)ma; i++)
+ r = rand();
+ pthread_mutex_lock((pthread_mutex_t *)m);
+ if (g_rfound == -1)
+ g_rfound = r;
+ pthread_mutex_unlock((pthread_mutex_t *)m);
+ return (NULL);
+}
+
+/*
+** Control how many threads to create,
+** the more threads the more secure ntrand wil be
+*/
+
+int ntrand(unsigned long long n)
+{
+ g_rfound = -1;
+ pthread_t threads[n];
+ for (unsigned long long i = 0; i < n; i++)
+ pthread_create(&(threads[i]), NULL, thread, (void *)(&i));
+ for (unsigned long long i = 0; i < n; i++)
+ pthread_join(threads[i], NULL);
+ return (g_rfound);
+}
+
+/*
+** Basically rand but we seed with time for you, user-friendly
+*/
+
+int strand()
+{
+ g_rfound = -1;
+ srand(time(NULL));
+ pthread_mutex_t mutex;
+ pthread_t threads[DEF_NTHREAD];
+ for (unsigned long long i = 0; i < DEF_NTHREAD; i++)
+ pthread_create(&(threads[i]), NULL, thread, (void *)(&i));
+ for (unsigned long long i = 0; i < DEF_NTHREAD; i++)
+ pthread_join(threads[i], NULL);
+ return (g_rfound);
+}
+
+
+/*
+* In this version you are responsable for seeding rand()
+*/
+
+int trand()
+{
+ g_rfound = -1;
+ pthread_mutex_t mutex;
+ pthread_t threads[DEF_NTHREAD];
+ for (unsigned long long i = 0; i < DEF_NTHREAD; i++)
+ pthread_create(&(threads[i]), NULL, thread, (void *)(&i));
+ for (unsigned long long i = 0; i < DEF_NTHREAD; i++)
+ pthread_join(threads[i], NULL);
+ return (g_rfound);
+}
diff --git a/trand.h b/trand.h
new file mode 100644
index 0000000..7286c45
--- /dev/null
+++ b/trand.h
@@ -0,0 +1,16 @@
+#ifndef TRAND_H
+ #define TRAND_H
+
+ #ifndef DEF_NTHREAD
+ #define DEF_NTHREAD 4
+ #endif
+
+#include <pthread.h>
+#include <time.h>
+#include <stdlib.h>
+
+int ntrand(unsigned long long n);
+int trand();
+int strand();
+
+#endif