add encrypting for save files

add libhydrogen for encrypting and decrypting

#9
This commit is contained in:
2026-04-03 14:59:37 +02:00
parent abb6556fd4
commit c277ab10d9
68 changed files with 5737 additions and 74 deletions
+198 -73
View File
@@ -3,6 +3,7 @@
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <hydrogen.h>
#include "inter.h"
#include "inter-bold.h"
@@ -52,7 +53,7 @@ int weekday_from_day(int year, int month, int day) {
int calendar_week_from_day(int year, int month, int day){
int days_sum[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
int days = days_sum[month - 1];
if(is_leap_year(year) && month > 2)
if (is_leap_year(year) && month > 2)
days++;
return (10 + days + day - weekday_from_day(year, month, day)) / 7;
@@ -66,7 +67,18 @@ int days_per_month(int year, int month){
return days_in_month[month - 1];
}
void init(){
#define HYDROGEN_CONTEXT "WorkCalS"
#define HYDROGEN_OPSLIMIT 10000
#define HYDROGEN_MEMLIMIT 0
#define HYDROGEN_THREADS 1
static uint8_t master_key[hydro_pwhash_MASTERKEYBYTES] = { 0x6c, 0x2e, 0xed, 0x47, 0x36, 0x29, 0xda, 0x11, 0x6e, 0xf4, 0x41, 0x66, 0x3b, 0xd7, 0xfa, 0x72, 0xf7, 0x51, 0x48, 0x6d, 0x10, 0x7b, 0xa5, 0x04, 0x00, 0x11, 0x2e, 0xc4, 0xf2, 0xdb, 0x77, 0x51 };
static uint8_t derived_key[hydro_secretbox_KEYBYTES];
static char password_input_buffer[256];
static char password_confirmation_input_buffer[256];
void init() {
hydro_init();
ImGuiIO &io = ImGui::GetIO();
inter_regular = io.Fonts->AddFontFromMemoryCompressedTTF(inter_compressed_data, inter_compressed_size);
inter_bold = io.Fonts->AddFontFromMemoryCompressedTTF(inter_bold_compressed_data, inter_bold_compressed_size);
@@ -103,68 +115,115 @@ static uint32_t num_categorized_days = 0;
static Categorized_Day categorized_days[365*50];
//Colors
ImVec4 table_white_bg = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
ImVec4 table_saturday_bg = ImVec4(0.0f, 0.0f, 0.0f, 0.1f);
ImVec4 table_sunday_bg = ImVec4(0.0f, 0.0f, 0.0f, 0.2f);
static ImVec4 table_white_bg = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
static ImVec4 table_saturday_bg = ImVec4(0.0f, 0.0f, 0.0f, 0.1f);
static ImVec4 table_sunday_bg = ImVec4(0.0f, 0.0f, 0.0f, 0.2f);
ImVec4 table_hover_color = ImVec4(0.0f, 0.3f, 0.6f, 0.2f);
static ImVec4 table_hover_color = ImVec4(0.0f, 0.3f, 0.6f, 0.2f);
static char plaintext_buffer[2 * 1024 * 1024] = {};
static char encrypted_buffer[hydro_secretbox_HEADERBYTES + sizeof(plaintext_buffer)];
void save(){
if(!save_file_path) return;
if (!save_file_path) return;
uint32_t version = 1;
FILE *save_file = fopen(save_file_path, "wb");
fwrite(&version, sizeof(version), 1, save_file);
fwrite(&num_categories, sizeof(num_categories), 1, save_file);
fwrite(&num_categorized_days, sizeof(num_categorized_days), 1, save_file);
#define write_to_buffer(x) memcpy(plaintext_buffer + offset, &(x), sizeof(x)); offset += sizeof(x);
size_t offset = 0;
write_to_buffer(version);
write_to_buffer(num_categories);
write_to_buffer(num_categorized_days);
for (int i = 0; i < num_categories; i++) {
fwrite(&categories[i].color, sizeof(categories[i].color), 1, save_file);
fwrite(&categories[i].name, sizeof(categories[i].name), 1, save_file);
write_to_buffer(categories[i].color);
write_to_buffer(categories[i].name);
}
for (int i = 0; i < num_categorized_days; i++) {
fwrite(&categorized_days[i].year, sizeof(categorized_days[i].year), 1, save_file);
fwrite(&categorized_days[i].month, sizeof(categorized_days[i].month), 1, save_file);
fwrite(&categorized_days[i].day, sizeof(categorized_days[i].day), 1, save_file);
fwrite(&categorized_days[i].category, sizeof(categorized_days[i].category), 1, save_file);
write_to_buffer(categorized_days[i].year);
write_to_buffer(categorized_days[i].month);
write_to_buffer(categorized_days[i].day);
write_to_buffer(categorized_days[i].category);
}
#undef write_to_buffer
if (hydro_secretbox_encrypt((uint8_t *)encrypted_buffer, plaintext_buffer, offset, 0, HYDROGEN_CONTEXT, derived_key) != 0) {
return; //TODO error message
}
hydro_memzero(plaintext_buffer, sizeof(plaintext_buffer));
FILE *save_file = fopen(save_file_path, "wb");
fwrite(encrypted_buffer, hydro_secretbox_HEADERBYTES + offset, 1, save_file);
fclose (save_file);
hydro_memzero(encrypted_buffer, sizeof(encrypted_buffer));
char *title = (char *)calloc(1, strlen("Work Calendar - ") + strlen(save_file_path) + 1);
memcpy(title, "Work Calendar - ", strlen("Work Calendar - "));
memcpy(title + strlen("Work Calendar - "), save_file_path, strlen(save_file_path));
set_window_title(title);
free(title);
}
void load(){
FILE *save_file = fopen(save_file_path, "rb");
uint32_t version = 0;
fread(&version, sizeof(version), 1, save_file);
if(version != 1) {
size_t num_bytes = fread(encrypted_buffer, 1, sizeof(encrypted_buffer), save_file);
fclose (save_file);
if ( num_bytes <= 0) {
return; //TODO Popup warning
}
fread(&num_categories, sizeof(num_categories), 1, save_file);
fread(&num_categorized_days, sizeof(num_categorized_days), 1, save_file);
if (hydro_secretbox_decrypt(plaintext_buffer, (uint8_t *)encrypted_buffer, num_bytes, 0, HYDROGEN_CONTEXT, derived_key) != 0){
return; //TODO Popup warning
}
hydro_memzero(encrypted_buffer, sizeof(encrypted_buffer));
if(num_categories > IM_ARRAYSIZE(categories) || num_categorized_days > IM_ARRAYSIZE(categorized_days)){
#define read_from_buffer(x) memcpy(&(x), plaintext_buffer + offset, sizeof(x)); offset += sizeof(x);
size_t offset = 0;
uint32_t version = 0;
read_from_buffer(version);
if (version != 1) {
return; //TODO Popup warning
}
read_from_buffer(num_categories);
read_from_buffer(num_categorized_days);
if (num_categories > IM_ARRAYSIZE(categories) || num_categorized_days > IM_ARRAYSIZE(categorized_days)){
return; //TODO Popup warning
}
for (int i = 0; i < num_categories; i++) {
fread(&categories[i].color, sizeof(categories[i].color), 1, save_file);
fread(&categories[i].name, sizeof(categories[i].name), 1, save_file);
read_from_buffer(categories[i].color);
read_from_buffer(categories[i].name);
}
for (int i = 0; i < num_categorized_days; i++) {
fread(&categorized_days[i].year, sizeof(categorized_days[i].year), 1, save_file);
fread(&categorized_days[i].month, sizeof(categorized_days[i].month), 1, save_file);
fread(&categorized_days[i].day, sizeof(categorized_days[i].day), 1, save_file);
fread(&categorized_days[i].category, sizeof(categorized_days[i].category), 1, save_file);
read_from_buffer(categorized_days[i].year);
read_from_buffer(categorized_days[i].month);
read_from_buffer(categorized_days[i].day);
read_from_buffer(categorized_days[i].category);
}
#undef read_from_buffer
fclose (save_file);
hydro_memzero(plaintext_buffer, sizeof(plaintext_buffer));
char *title = (char *)calloc(1, strlen("Work Calendar - ") + strlen(save_file_path) + 1);
memcpy(title, "Work Calendar - ", strlen("Work Calendar - "));
memcpy(title + strlen("Work Calendar - "), save_file_path, strlen(save_file_path));
set_window_title(title);
free(title);
}
void per_frame(){
ImGuiStyle &style = ImGui::GetStyle();
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
year_min_size = ImGui::CalcTextSize("8888").x + 5;
@@ -183,26 +242,17 @@ void per_frame(){
ImGui::DockSpaceOverViewport(main_viewport_dock, ImGui::GetMainViewport(), ImGuiDockNodeFlags_NoTabBar);
ImGuiID close_popup = ImGui::GetID("Close");
ImGuiID open_popup = ImGui::GetID("Open");
ImGuiID save_popup = ImGui::GetID("Save");
ImGuiID logout_popup = ImGui::GetID("Logout"); //TODO
ImGuiID about_popup = ImGui::GetID("About");
ImGuiID save_password_popup = ImGui::GetID("Passwort eingeben##Save");
ImGuiID open_password_popup = ImGui::GetID("Passwort eingeben##Open");
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("Datei")) {
if (ImGui::MenuItem("Öffnen", "Ctrl + O")) {
char *new_save_file_path = open_file_dialog();
if(new_save_file_path) {
if(save_file_path) free(save_file_path);
if (new_save_file_path) {
if (save_file_path) free(save_file_path);
save_file_path = new_save_file_path;
load();
char *title = (char *)calloc(1, strlen("Work Calendar - ") + strlen(save_file_path) + 1);
memcpy(title, "Work Calendar - ", strlen("Work Calendar - "));
memcpy(title + strlen("Work Calendar - "), save_file_path, strlen(save_file_path));
set_window_title(title);
free(title);
ImGui::OpenPopup(open_password_popup);
}
}
if (ImGui::MenuItem("Speichern", "Ctrl + S", false, !!save_file_path)) {
@@ -211,23 +261,27 @@ void per_frame(){
if (ImGui::MenuItem("Speichern unter", "Ctrl + Shift + S")) {
char *new_save_file_path = save_file_dialog();
if(new_save_file_path) {
if(save_file_path) free(save_file_path);
if (new_save_file_path) {
if (save_file_path) free(save_file_path);
save_file_path = new_save_file_path;
save();
char *title = (char *)calloc(1, strlen("Work Calendar - ") + strlen(save_file_path) + 1);
memcpy(title, "Work Calendar - ", strlen("Work Calendar - "));
memcpy(title + strlen("Work Calendar - "), save_file_path, strlen(save_file_path));
set_window_title(title);
free(title);
ImGui::OpenPopup(save_password_popup);
}
}
//TODO
ImGui::Separator();
ImGui::MenuItem("Kalender Schließen", "Ctrl + X");
if (ImGui::MenuItem("Kalender Schließen", "Ctrl + X")) {
//TODO if calendar is opened warning
if (save_file_path) free(save_file_path);
save_file_path = NULL;
num_categories = 0;
num_categorized_days = 0;
hydro_memzero(categories, sizeof(categories));
hydro_memzero(categorized_days, sizeof(categorized_days));
hydro_memzero(derived_key, sizeof(derived_key));
set_window_title("Work Calendar");
}
ImGui::Separator();
if (ImGui::MenuItem("Beenden", NULL)) {
@@ -254,7 +308,78 @@ void per_frame(){
ImGui::EndMainMenuBar();
}
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Passwort eingeben##Save", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f);
ImGui::AlignTextToFramePadding();
ImGui::Text("Passwort");
ImGui::SameLine();
ImGui::SetNextItemWidth(-1);
ImGui::InputText("##Passwort", password_input_buffer, sizeof(password_input_buffer), ImGuiInputTextFlags_Password);
ImGui::AlignTextToFramePadding();
ImGui::Text("Passwort bestätigen");
ImGui::SameLine();
ImGui::SetNextItemWidth(-1);
ImGui::InputText("##Passwort bestätigen", password_confirmation_input_buffer, sizeof(password_confirmation_input_buffer), ImGuiInputTextFlags_Password);
ImGui::PopStyleVar();
if (ImGui::Button("OK", ImVec2(120, 0))) {
if (hydro_compare((uint8_t *)password_input_buffer, (uint8_t *) password_confirmation_input_buffer, sizeof(password_input_buffer)) == 0) {
hydro_pwhash_deterministic(derived_key, sizeof derived_key, password_input_buffer, strlen(password_input_buffer), HYDROGEN_CONTEXT,
master_key, HYDROGEN_OPSLIMIT, HYDROGEN_MEMLIMIT, HYDROGEN_THREADS);
save();
ImGui::CloseCurrentPopup();
hydro_memzero(password_input_buffer, sizeof(password_input_buffer));
hydro_memzero(password_confirmation_input_buffer, sizeof(password_confirmation_input_buffer));
}
//TODO color input red + message
}
ImGui::SameLine();
if (ImGui::Button("Abbrechen", ImVec2(120, 0))) {
ImGui::CloseCurrentPopup();
hydro_memzero(password_input_buffer, sizeof(password_input_buffer));
hydro_memzero(password_confirmation_input_buffer, sizeof(password_confirmation_input_buffer));
}
ImGui::EndPopup();
}
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Passwort eingeben##Open", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f);
ImGui::AlignTextToFramePadding();
ImGui::Text("Passwort");
ImGui::SameLine();
ImGui::SetNextItemWidth(-1);
ImGui::InputText("##Passwort", password_input_buffer, sizeof(password_input_buffer), ImGuiInputTextFlags_Password);
ImGui::PopStyleVar();
if (ImGui::Button("OK", ImVec2(120, 0))) {
hydro_pwhash_deterministic(derived_key, sizeof derived_key, password_input_buffer, strlen(password_input_buffer), HYDROGEN_CONTEXT,
master_key, HYDROGEN_OPSLIMIT, HYDROGEN_MEMLIMIT, HYDROGEN_THREADS);
hydro_memzero(password_input_buffer, sizeof(password_input_buffer));
load();
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("Abbrechen", ImVec2(120, 0))) {
if (save_file_path) free(save_file_path);
save_file_path = NULL;
ImGui::CloseCurrentPopup();
hydro_memzero(password_input_buffer, sizeof(password_input_buffer));
}
ImGui::EndPopup();
}
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Close", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::Text("Möchten Sie wirklich beenden?");
@@ -278,7 +403,7 @@ void per_frame(){
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, {0,0,0,0});
ImGui::PushStyleColor(ImGuiCol_HeaderActive, {0,0,0,0});
if(ImGui::BeginTable("CalendarHeader", 5)) {
if (ImGui::BeginTable("CalendarHeader", 5)) {
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch); // Left side
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed); // Prev year
@@ -309,7 +434,7 @@ void per_frame(){
ImGui::PushStyleColor(ImGuiCol_TableBorderStrong, {0,0,0,0});
ImGui::PushStyleColor(ImGuiCol_TableBorderLight, {0,0,0,0});
if(ImGui::BeginTable("Month", 1, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX)) {
if (ImGui::BeginTable("Month", 1, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX)) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextAligned(0.5, -FLT_MIN, month_names[month - 1]);
@@ -318,7 +443,7 @@ void per_frame(){
ImGui::TableSetColumnIndex(0);
//calendar week table
if(ImGui::BeginTable("CalendarWeek", 1, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)){
if (ImGui::BeginTable("CalendarWeek", 1, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)){
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextAligned(0.5, -FLT_MIN, "KW");
@@ -326,13 +451,13 @@ void per_frame(){
for (int day = 1; day <= days_per_month(year, month); day++) {
int weekday = weekday_from_day(year, month, day);
if(weekday == 0 || day == 1){
if (weekday == 0 || day == 1){
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
//if first week has 3 or less days, last week from previous year
int calender_week = calendar_week_from_day(year, month, day);
if(calender_week == 0) {
if (calender_week == 0) {
ImGui::TextAligned(0.5, -FLT_MIN, "%d", 53);
} else {
ImGui::TextAligned(0.5, -FLT_MIN, "%d", calender_week);
@@ -347,7 +472,7 @@ void per_frame(){
ImGui::BeginGroup();
ImGui::PushStyleVarY(ImGuiStyleVar_ItemSpacing, 0);
if(ImGui::BeginTable("Weekdays", 7, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)){
if (ImGui::BeginTable("Weekdays", 7, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)){
ImGui::TableSetupColumn("Mo");
ImGui::TableSetupColumn("Di");
@@ -366,13 +491,13 @@ void per_frame(){
ImGui::PushStyleColor(ImGuiCol_TableBorderLight, {119/255.0f,119/255.0f,119/255.0f,83/255.0f});
//days of month table
if(ImGui::BeginTable("Weekdays", 7, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)) {
if (ImGui::BeginTable("Weekdays", 7, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)) {
int offset = weekday_from_day(year, month, 1);
for (int day = 1; day <= days_per_month(2025, month); day++) {
int weekday = weekday_from_day(year, month, day);
if(weekday == 0 || day == 1){
if (weekday == 0 || day == 1){
ImGui::TableNextRow();
}
@@ -381,12 +506,12 @@ void per_frame(){
ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_workweek);
//weekend coloring
if(weekday == 5){
if (weekday == 5){
ImU32 cell_bg_color_sat = ImGui::GetColorU32(table_saturday_bg);
ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_sat);
}
if(weekday == 6){
if (weekday == 6){
ImU32 cell_bg_color_sun = ImGui::GetColorU32(table_sunday_bg);
ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_sun);
}
@@ -401,15 +526,15 @@ void per_frame(){
color.Value.w = 0.5;
//sat + sun: add alpha value
if(weekday == 5)
if (weekday == 5)
color.Value.w = color.Value.w + table_saturday_bg.w;
if(weekday == 6)
if (weekday == 6)
color.Value.w = color.Value.w + table_sunday_bg.w;
ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ImU32(color));
}
if(current_year == year && current_month == month && current_day == day ) {
if (current_year == year && current_month == month && current_day == day ) {
ImGui::PushFont(inter_bold);
ImGui::TextAligned(0.5, -FLT_MIN, "%d", day);
ImGui::PopFont();
@@ -417,8 +542,8 @@ void per_frame(){
ImGui::TextAligned(0.5, -FLT_MIN, "%d", day);
}
if(ImGui::IsItemHovered()){
if(selected_category != -1) {
if (ImGui::IsItemHovered()){
if (selected_category != -1) {
ImColor hover_color = categories[selected_category].color;
hover_color.Value.w = 0.2;
ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ImGui::GetColorU32(ImU32(hover_color)));
@@ -485,7 +610,7 @@ void per_frame(){
// Legende
if (legend_visible && ImGui::Begin("Legende", 0)) {
if(ImGui::BeginTable("Legend", 4, ImGuiTableFlags_SizingStretchProp)) {
if (ImGui::BeginTable("Legend", 4, ImGuiTableFlags_SizingStretchProp)) {
ImGui::TableSetupColumn("Color", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("Edit", ImGuiTableColumnFlags_WidthFixed);
@@ -506,7 +631,7 @@ void per_frame(){
ImGui::TableSetColumnIndex(1);
if (category.editing) {
ImGui::SetNextItemWidth(-1);
if(ImGui::InputText("##name", category.name, IM_ARRAYSIZE(category.name), ImGuiInputTextFlags_EnterReturnsTrue)) {
if (ImGui::InputText("##name", category.name, IM_ARRAYSIZE(category.name), ImGuiInputTextFlags_EnterReturnsTrue)) {
category.editing = false;
}
} else {