Skip to content

Commit

Permalink
Add SSL utility functions for HMAC and SHA-256, and update CMake conf…
Browse files Browse the repository at this point in the history
…iguration
  • Loading branch information
royshil committed Dec 6, 2024
1 parent 4c8ef32 commit 8c12d98
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 87 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ target_sources(
src/cloud-providers/cloud-provider.cpp
src/cloud-providers/clova/clova-provider.cpp
src/cloud-providers/google/google-provider.cpp
src/ssl-utils/ssl-utils.cpp
src/timed-metadata/timed-metadata-utils.cpp)

add_subdirectory(src/cloud-translation)
Expand Down
102 changes: 102 additions & 0 deletions src/ssl-utils/ssl-utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "ssl-utils.h"

#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/err.h>

#include <vector>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <ctime>

#include <util/bmem.h>

#include "plugin-support.h"

void init_openssl()
{
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
}

// HMAC SHA-256 function
std::string hmacSha256(const std::string &key, const std::string &data, bool isHexKey)
{
unsigned char *digest = (unsigned char *)bzalloc(EVP_MAX_MD_SIZE);
size_t len = EVP_MAX_MD_SIZE;

// Prepare the key
std::vector<unsigned char> keyBytes;
if (isHexKey) {
for (size_t i = 0; i < key.length(); i += 2) {
std::string byteString = key.substr(i, 2);
unsigned char byte = (unsigned char)strtol(byteString.c_str(), NULL, 16);
keyBytes.push_back(byte);
}
} else {
keyBytes.assign(key.begin(), key.end());
}

if (!HMAC(EVP_sha256(), keyBytes.data(), keyBytes.size(), (unsigned char *)data.c_str(),
data.length(), digest, (unsigned int *)&len)) {
obs_log(LOG_ERROR, "hmacSha256 failed during HMAC operation");
return {};
}

std::stringstream ss;
for (size_t i = 0; i < len; ++i) {
ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
}

bfree(digest);
return ss.str();
}

std::string sha256(const std::string &data)
{
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int lengthOfHash = 0;

EVP_MD_CTX *context = EVP_MD_CTX_new();

if (context != nullptr) {
if (EVP_DigestInit_ex(context, EVP_sha256(), nullptr)) {
if (EVP_DigestUpdate(context, data.c_str(), data.length())) {
if (EVP_DigestFinal_ex(context, hash, &lengthOfHash)) {
EVP_MD_CTX_free(context);

std::stringstream ss;
for (unsigned int i = 0; i < lengthOfHash; ++i) {
ss << std::hex << std::setw(2) << std::setfill('0')
<< (int)hash[i];
}
return ss.str();
}
}
}
EVP_MD_CTX_free(context);
}

return "";
}

std::string getCurrentTimestamp()
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::gmtime(&in_time_t), "%Y%m%dT%H%M%SZ");
return ss.str();
}

std::string getCurrentDate()
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::gmtime(&in_time_t), "%Y%m%d");
return ss.str();
}
9 changes: 9 additions & 0 deletions src/ssl-utils/ssl-utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <string>

void init_openssl();
std::string hmacSha256(const std::string &key, const std::string &data, bool isHexKey = false);
std::string sha256(const std::string &data);
std::string getCurrentTimestamp();
std::string getCurrentDate();
86 changes: 1 addition & 85 deletions src/timed-metadata/timed-metadata-utils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "plugin-support.h"
#include "timed-metadata-utils.h"
#include "ssl-utils/ssl-utils.h"

#include <openssl/evp.h>
#include <openssl/sha.h>
Expand All @@ -21,91 +22,6 @@

#include <nlohmann/json.hpp>

void init_openssl()
{
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
}

// HMAC SHA-256 function
std::string hmacSha256(const std::string &key, const std::string &data, bool isHexKey = false)
{
unsigned char *digest = (unsigned char *)bzalloc(EVP_MAX_MD_SIZE);
size_t len = EVP_MAX_MD_SIZE;

// Prepare the key
std::vector<unsigned char> keyBytes;
if (isHexKey) {
for (size_t i = 0; i < key.length(); i += 2) {
std::string byteString = key.substr(i, 2);
unsigned char byte = (unsigned char)strtol(byteString.c_str(), NULL, 16);
keyBytes.push_back(byte);
}
} else {
keyBytes.assign(key.begin(), key.end());
}

if (!HMAC(EVP_sha256(), keyBytes.data(), keyBytes.size(), (unsigned char *)data.c_str(),
data.length(), digest, (unsigned int *)&len)) {
obs_log(LOG_ERROR, "hmacSha256 failed during HMAC operation");
return {};
}

std::stringstream ss;
for (size_t i = 0; i < len; ++i) {
ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
}

bfree(digest);
return ss.str();
}

std::string sha256(const std::string &data)
{
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int lengthOfHash = 0;

EVP_MD_CTX *context = EVP_MD_CTX_new();

if (context != nullptr) {
if (EVP_DigestInit_ex(context, EVP_sha256(), nullptr)) {
if (EVP_DigestUpdate(context, data.c_str(), data.length())) {
if (EVP_DigestFinal_ex(context, hash, &lengthOfHash)) {
EVP_MD_CTX_free(context);

std::stringstream ss;
for (unsigned int i = 0; i < lengthOfHash; ++i) {
ss << std::hex << std::setw(2) << std::setfill('0')
<< (int)hash[i];
}
return ss.str();
}
}
}
EVP_MD_CTX_free(context);
}

return "";
}

std::string getCurrentTimestamp()
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::gmtime(&in_time_t), "%Y%m%dT%H%M%SZ");
return ss.str();
}

std::string getCurrentDate()
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::gmtime(&in_time_t), "%Y%m%d");
return ss.str();
}

size_t WriteCallback(void *ptr, size_t size, size_t nmemb, std::string *data)
{
data->append((char *)ptr, size * nmemb);
Expand Down
2 changes: 0 additions & 2 deletions src/timed-metadata/timed-metadata-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ void send_timed_metadata_to_server(struct cloudvocal_data *gf, Translation_Mode
const std::string &source_text, const std::string &source_lang,
const std::string &target_text, const std::string &target_lang);

void init_openssl();

#endif // TIMED_METADATA_UTILS_H

0 comments on commit 8c12d98

Please sign in to comment.