ClickHouse/src/Common/OpenSSLHelpers.cpp

50 lines
1.1 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"
#include <ext/scope_guard.h>
#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()
{
BIO * mem = BIO_new(BIO_s_mem());
SCOPE_EXIT(BIO_free(mem));
ERR_print_errors(mem);
char * buf = nullptr;
2020-03-18 03:27:32 +00:00
size_t size = BIO_get_mem_data(mem, &buf);
2019-05-26 06:52:29 +00:00
return String(buf, size);
}
}
2019-09-11 11:21:54 +00:00
#endif