aboutsummaryrefslogtreecommitdiff
path: root/src/StringUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/StringUtils.cpp')
-rw-r--r--src/StringUtils.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp
index 656b511..57c30d4 100644
--- a/src/StringUtils.cpp
+++ b/src/StringUtils.cpp
@@ -1,5 +1,7 @@
#include "../include/StringUtils.hpp"
#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
namespace QuickMedia {
template <typename T>
@@ -333,4 +335,29 @@ namespace QuickMedia {
return result;
}
+
+ bool generate_random_characters(char *buffer, int buffer_size, const char *alphabet, size_t alphabet_size) {
+ if(alphabet_size == 0)
+ return false;
+
+ int fd = open("/dev/urandom", O_RDONLY);
+ if(fd == -1) {
+ perror("/dev/urandom");
+ return false;
+ }
+
+ if(read(fd, buffer, buffer_size) < buffer_size) {
+ fprintf(stderr, "Failed to read %d bytes from /dev/urandom\n", buffer_size);
+ close(fd);
+ return false;
+ }
+
+ for(int i = 0; i < buffer_size; ++i) {
+ unsigned char c = *(unsigned char*)&buffer[i];
+ buffer[i] = alphabet[c % alphabet_size];
+ }
+
+ close(fd);
+ return true;
+ }
} \ No newline at end of file