2022-09-28 08:45:15 +00:00
|
|
|
#include "config.h"
|
2020-08-02 20:44:59 +00:00
|
|
|
|
2019-10-05 19:25:31 +00:00
|
|
|
#if USE_SSL
|
2019-05-26 06:52:29 +00:00
|
|
|
#include "OpenSSLHelpers.h"
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/scope_guard.h>
|
2019-05-26 06:52:29 +00:00
|
|
|
#include <openssl/err.h>
|
2020-02-06 01:48:14 +00:00
|
|
|
#include <openssl/sha.h>
|
2019-05-26 06:52:29 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2019-12-15 06:34:43 +00:00
|
|
|
#pragma GCC diagnostic warning "-Wold-style-cast"
|
2019-05-26 06:52:29 +00:00
|
|
|
|
2022-07-14 16:11:35 +00:00
|
|
|
std::string encodeSHA256(std::string_view text)
|
2020-09-10 18:24:53 +00:00
|
|
|
{
|
|
|
|
return encodeSHA256(text.data(), text.size());
|
|
|
|
}
|
|
|
|
std::string encodeSHA256(const void * text, size_t size)
|
|
|
|
{
|
|
|
|
std::string out;
|
|
|
|
out.resize(32);
|
|
|
|
encodeSHA256(text, size, reinterpret_cast<unsigned char *>(out.data()));
|
|
|
|
return out;
|
|
|
|
}
|
2022-07-14 16:11:35 +00:00
|
|
|
void encodeSHA256(std::string_view text, unsigned char * out)
|
2020-09-10 18:24:53 +00:00
|
|
|
{
|
|
|
|
encodeSHA256(text.data(), text.size(), out);
|
|
|
|
}
|
|
|
|
void encodeSHA256(const void * text, size_t size, unsigned char * out)
|
2020-02-06 01:48:14 +00:00
|
|
|
{
|
|
|
|
SHA256_CTX ctx;
|
|
|
|
SHA256_Init(&ctx);
|
2020-09-10 18:24:53 +00:00
|
|
|
SHA256_Update(&ctx, reinterpret_cast<const UInt8 *>(text), size);
|
2020-02-06 01:48:14 +00:00
|
|
|
SHA256_Final(out, &ctx);
|
|
|
|
}
|
|
|
|
|
2019-05-26 06:52:29 +00:00
|
|
|
String getOpenSSLErrors()
|
|
|
|
{
|
2020-10-15 19:54:52 +00:00
|
|
|
String res;
|
|
|
|
ERR_print_errors_cb([](const char * str, size_t len, void * ctx)
|
|
|
|
{
|
|
|
|
String & out = *reinterpret_cast<String*>(ctx);
|
|
|
|
if (!out.empty())
|
|
|
|
out += ", ";
|
|
|
|
out.append(str, len);
|
|
|
|
return 1;
|
|
|
|
}, &res);
|
|
|
|
return res;
|
2019-05-26 06:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-09-11 11:21:54 +00:00
|
|
|
#endif
|