rublon-ssh-old/PAM/ssh/include/rublon/sign.hpp
2024-06-17 12:50:06 +02:00

45 lines
1.2 KiB
C++
Executable File

#pragma once
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/sha.h>
#include <rublon/utils.hpp>
namespace rublon {
inline StaticString< SHA256_DIGEST_LENGTH * 2> SHA256(const char * const path) {
std::string fileContent;
readFile(path, fileContent);
StaticString< SHA256_DIGEST_LENGTH * 2 > xRublon{};
std::array< unsigned char, SHA256_DIGEST_LENGTH + 1 > hash{};
SHA256_CTX ctx;
SHA256_Init(&ctx);
SHA256_Update(&ctx, fileContent.data(), fileContent.size());
SHA256_Final(hash.data(), &ctx);
for(unsigned int i = 0; i < SHA256_DIGEST_LENGTH; i++)
sprintf(&xRublon[i * 2], "%02x", ( unsigned int ) hash[i]);
return xRublon;
}
// +1 for \0
inline StaticString< 64> signData(std::string_view data, std::string_view secretKey) {
StaticString< 64 > xRublon;
std::array< unsigned char, EVP_MAX_MD_SIZE > md;
unsigned int md_len{};
HMAC(EVP_sha256(), secretKey.data(), secretKey.size(), ( unsigned const char * ) data.data(), data.size(), md.data(), &md_len);
for(unsigned int i = 0; i < md_len; i++)
sprintf(&xRublon[i * 2], "%02x", ( unsigned int ) md[i]);
return xRublon;
}
} // namespace rublon