aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Matrix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/Matrix.cpp')
-rw-r--r--src/plugins/Matrix.cpp41
1 files changed, 9 insertions, 32 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp
index b9fbea0..7bee2ab 100644
--- a/src/plugins/Matrix.cpp
+++ b/src/plugins/Matrix.cpp
@@ -15,7 +15,6 @@
#include <rapidjson/stringbuffer.h>
#include <rapidjson/filereadstream.h>
#include <rapidjson/filewritestream.h>
-#include <fcntl.h>
#include <unistd.h>
#include <fstream>
#include <malloc.h>
@@ -258,6 +257,10 @@ namespace QuickMedia {
return std::abs(hash);
}
+ static bool generate_random_string_readable(char *buffer, int buffer_size) {
+ return generate_random_characters(buffer, buffer_size, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62);
+ }
+
mgl::Color user_id_to_color(const std::string &user_id) {
const int num_colors = 8;
const mgl::Color colors[num_colors] = {
@@ -1233,31 +1236,6 @@ namespace QuickMedia {
return filepath.c_str() + index + 1;
}
- static bool generate_random_characters(char *buffer, int buffer_size) {
- 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;
- }
-
- close(fd);
- return true;
- }
-
- static std::string random_characters_to_readable_string(const char *buffer, int buffer_size) {
- std::ostringstream result;
- result << std::hex;
- for(int i = 0; i < buffer_size; ++i)
- result << (int)(unsigned char)buffer[i];
- return result.str();
- }
-
PluginResult MatrixCustomEmojiPage::submit(const SubmitArgs &args, std::vector<Tab> &result_tabs) {
if(args.url == "add") {
auto submit_handler = [this](FileManagerPage*, const std::filesystem::path &filepath) {
@@ -1270,12 +1248,11 @@ namespace QuickMedia {
}
if(key.empty()) {
- char random_characters[10];
- if(!generate_random_characters(random_characters, sizeof(random_characters))) {
+ key.resize(10);
+ if(!generate_random_string_readable(key.data(), key.size())) {
show_notification("QuickMedia", "Failed to generate random string", Urgency::CRITICAL);
return false;
}
- key = random_characters_to_readable_string(random_characters, sizeof(random_characters));
}
if(matrix->does_custom_emoji_with_name_exist(key)) {
@@ -3996,11 +3973,11 @@ namespace QuickMedia {
}
std::string create_transaction_id() {
- char random_characters[18];
- if(!generate_random_characters(random_characters, sizeof(random_characters)))
+ std::string random_readable_chars;
+ random_readable_chars.resize(18);
+ if(!generate_random_string_readable(random_readable_chars.data(), random_readable_chars.size()))
return "";
- std::string random_readable_chars = random_characters_to_readable_string(random_characters, sizeof(random_characters));
return "m." + std::to_string(time(NULL)) + random_readable_chars;
}