From 98bc18cedd486961fb555ea8fdc1abc54c23a3d4 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Tue, 27 Apr 2021 17:24:40 -0700 Subject: [PATCH] utils: Create Utils namespace and add Crypto utils --- CMakeLists.txt | 3 +- src/utils/Crypto.cpp | 55 +++++++++++++++++++++++++++ src/utils/{JsonUtils.cpp => Json.cpp} | 0 src/utils/Utils.h | 7 ++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/utils/Crypto.cpp rename src/utils/{JsonUtils.cpp => Json.cpp} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 49bddd2e..e0031816 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,7 +78,8 @@ set(obs-websocket_SOURCES src/requesthandler/rpc/Request.cpp src/requesthandler/rpc/RequestResult.cpp src/forms/SettingsDialog.cpp - src/utils/JsonUtils.cpp) + src/utils/Json.cpp + src/utils/Crypto.cpp) set(obs-websocket_HEADERS src/obs-websocket.h diff --git a/src/utils/Crypto.cpp b/src/utils/Crypto.cpp new file mode 100644 index 00000000..ec28e4d1 --- /dev/null +++ b/src/utils/Crypto.cpp @@ -0,0 +1,55 @@ +#include +#include + +#include "Utils.h" + +#include "../plugin-macros.generated.h" + +QString Utils::Crypto::GenerateSalt() +{ + // Generate 32 random chars + const size_t randomCount = 32; + QByteArray randomChars; + for (size_t i = 0; i < randomCount; i++) { + randomChars.append((char)qrand()); + } + + // Convert the 32 random chars to a base64 string + return randomChars.toBase64(); +} + +QString Utils::Crypto::GenerateSecret(QString password, QString salt) +{ + // Concatenate the password and the salt + QString passAndSalt = ""; + passAndSalt += password; + passAndSalt += salt; + + // Generate a SHA256 hash of the password and salt + auto challengeHash = QCryptographicHash::hash( + passAndSalt.toUtf8(), + QCryptographicHash::Algorithm::Sha256 + ); + + // Encode SHA256 hash to Base64 + return challengeHash.toBase64(); +} + +bool Utils::Crypto::CheckAuthenticationString(QString secret, QString challenge, QString authenticationString) +{ + // Concatenate auth secret with the challenge sent to the user + QString secretAndChallenge = ""; + secretAndChallenge += secret; + secretAndChallenge += challenge; + + // Generate a SHA256 hash of secretAndChallenge + auto hash = QCryptographicHash::hash( + secretAndChallenge.toUtf8(), + QCryptographicHash::Algorithm::Sha256 + ); + + // Encode the SHA256 hash to Base64 + QString expectedAuthenticationString = hash.toBase64(); + + return (authenticationString == expectedAuthenticationString); +} diff --git a/src/utils/JsonUtils.cpp b/src/utils/Json.cpp similarity index 100% rename from src/utils/JsonUtils.cpp rename to src/utils/Json.cpp diff --git a/src/utils/Utils.h b/src/utils/Utils.h index b7889bbc..6b650bcf 100644 --- a/src/utils/Utils.h +++ b/src/utils/Utils.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -11,4 +12,10 @@ namespace Utils { obs_data_t *JsonToObsData(json j); json ObsDataToJson(obs_data_t *d, bool includeDefault = false); }; + + namespace Crypto { + QString GenerateSalt(); + QString GenerateSecret(QString password, QString salt); + bool CheckAuthenticationString(QString secret, QString challenge, QString authenticationString); + }; };