ClickHouse/src/Common/OpenSSLHelpers.cpp

54 lines
1.2 KiB
C++
Raw Normal View History

#if !defined(ARCADIA_BUILD)
# include <Common/config.h>
#endif
#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>
#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
std::string encodeSHA256(const std::string_view & text)
{
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;
}
void encodeSHA256(const std::string_view & text, unsigned char * out)
{
encodeSHA256(text.data(), text.size(), out);
}
void encodeSHA256(const void * text, size_t size, unsigned char * out)
{
SHA256_CTX ctx;
SHA256_Init(&ctx);
SHA256_Update(&ctx, reinterpret_cast<const UInt8 *>(text), size);
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