2020-08-02 20:44:59 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include <Common/config.h>
|
|
|
|
#endif
|
|
|
|
|
2019-10-05 19:25:31 +00:00
|
|
|
#if USE_SSL
|
2019-05-26 06:52:29 +00:00
|
|
|
#include "OpenSSLHelpers.h"
|
|
|
|
#include <ext/scope_guard.h>
|
|
|
|
#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
|
|
|
|
2020-09-10 18:24:53 +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;
|
|
|
|
}
|
2020-02-06 01:48:14 +00:00
|
|
|
void encodeSHA256(const 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()
|
|
|
|
{
|
|
|
|
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
|