mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
utils: Create Utils namespace and add Crypto utils
This commit is contained in:
parent
98ec9d01ac
commit
98bc18cedd
@ -78,7 +78,8 @@ set(obs-websocket_SOURCES
|
|||||||
src/requesthandler/rpc/Request.cpp
|
src/requesthandler/rpc/Request.cpp
|
||||||
src/requesthandler/rpc/RequestResult.cpp
|
src/requesthandler/rpc/RequestResult.cpp
|
||||||
src/forms/SettingsDialog.cpp
|
src/forms/SettingsDialog.cpp
|
||||||
src/utils/JsonUtils.cpp)
|
src/utils/Json.cpp
|
||||||
|
src/utils/Crypto.cpp)
|
||||||
|
|
||||||
set(obs-websocket_HEADERS
|
set(obs-websocket_HEADERS
|
||||||
src/obs-websocket.h
|
src/obs-websocket.h
|
||||||
|
55
src/utils/Crypto.cpp
Normal file
55
src/utils/Crypto.cpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#include <QByteArray>
|
||||||
|
#include <QCryptographicHash>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <obs-data.h>
|
#include <obs-data.h>
|
||||||
|
|
||||||
@ -11,4 +12,10 @@ namespace Utils {
|
|||||||
obs_data_t *JsonToObsData(json j);
|
obs_data_t *JsonToObsData(json j);
|
||||||
json ObsDataToJson(obs_data_t *d, bool includeDefault = false);
|
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);
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user