17#include <openssl/evp.h>
18#include <openssl/conf.h>
20#if OPENSSL_API_LEVEL < 30000
21#include <openssl/engine.h>
23#include <openssl/provider.h>
32#include <zypp-core/AutoDispose.h>
33#include <zypp-core/Digest.h>
34#include <zypp-core/base/Logger.h>
35#include <zypp-core/base/PtrTypes.h>
42 {
static std::string _type(
"md5" );
return _type; }
45 {
static std::string _type(
"sha1" );
return _type; }
48 {
static std::string _type(
"sha224" );
return _type; }
51 {
static std::string _type(
"sha256" );
return _type; }
54 {
static std::string _type(
"sha384" );
return _type; }
57 {
static std::string _type(
"sha512" );
return _type; }
62 P(
const P& p) =
delete;
73#if OPENSSL_API_LEVEL >= 30000
74 AutoDispose<EVP_MD *>
md;
78 unsigned char md_value[EVP_MAX_MD_SIZE];
106 bool Digest::P::maybeInit()
108 if(!openssl_digests_added)
110#if OPENSSL_API_LEVEL >= 30000
113 OPENSSL_init_crypto( OPENSSL_INIT_LOAD_CONFIG,
nullptr );
116 if ( !OSSL_PROVIDER_load(
nullptr,
"legacy" ) ) {
117 ERR <<
"Failed to load legacy openssl provider" << std::endl;
119 if ( !OSSL_PROVIDER_load(
nullptr,
"default") ) {
120 ERR <<
"Failed to load default openssl provider" << std::endl;
123 OPENSSL_init_crypto( OPENSSL_INIT_ADD_ALL_DIGESTS,
nullptr );
125# if OPENSSL_VERSION_NUMBER >= 0x10100000L
126 OPENSSL_init_crypto( OPENSSL_INIT_LOAD_CONFIG,
nullptr );
128 OPENSSL_config(NULL);
130 ENGINE_load_builtin_engines();
131 ENGINE_register_all_complete();
132 OpenSSL_add_all_digests();
134 openssl_digests_added =
true;
139#if OPENSSL_API_LEVEL >= 30000
142 md = AutoDispose<EVP_MD *>( EVP_MD_fetch (
nullptr, name.c_str(),
nullptr), EVP_MD_free );
144 md = EVP_get_digestbyname(name.c_str());
149#if OPENSSL_VERSION_NUMBER < 0x10100000L
150 EvpDataPtr tmp_mdctx(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
152 EvpDataPtr tmp_mdctx(EVP_MD_CTX_new(), EVP_MD_CTX_free);
157 if (!EVP_DigestInit_ex(tmp_mdctx.get(), md, NULL)) {
162 ::memset(md_value, 0,
sizeof(md_value));
166 mdctx.swap(tmp_mdctx);
171 void Digest::P::cleanup()
173#if OPENSSL_API_LEVEL >= 30000
180 Digest::Digest() : _dp(
std::make_unique<P>() )
184 Digest::Digest(Digest &&other) noexcept : _dp( std::move(other._dp) )
188 Digest &Digest::operator=(Digest &&other)
noexcept
190 _dp = std::move( other._dp );
197 bool Digest::create(
const std::string& name)
199 if(name.empty())
return false;
206 return _dp->maybeInit();
209 const std::string& Digest::name()
220 (void)EVP_DigestFinal_ex(_dp->mdctx.get(), _dp->md_value, &_dp->md_len);
221 _dp->finalized =
true;
223 if(!EVP_DigestInit_ex(_dp->mdctx.get(), _dp->md, NULL))
225 _dp->finalized =
false;
226 _dp->bytesHashed = 0;
230 Digest Digest::clone()
const
233 if ( !_dp->name.empty () )
234 d.create ( _dp->name );
238 std::string Digest::digest()
240 return digestVectorToString( digestVector() );
243 std::string Digest::digestVectorToString(
const UByteArray &vec)
246 return std::string();
248 std::vector<char> resData ( vec.size()*2 + 1,
'\0' );
249 char *mdtxt = &resData[0];
250 for(
unsigned i = 0; i < vec.size(); ++i)
252 ::snprintf( mdtxt+(i*2), 3,
"%02hhx", vec[i]);
254 return std::string( resData.data() );
257#ifdef __cpp_lib_string_view
259 template <
typename BArr>
260 BArr hexStrToBArr ( std::string_view
str ) {
262 for ( std::string::size_type i = 0; i <
str.length(); i+=2 )
264 #define c2h(c) (((c)>='0' && (c)<='9') ? ((c)-'0') \
265 : ((c)>='a' && (c)<='f') ? ((c)-('a'-10)) \
266 : ((c)>='A' && (c)<='F') ? ((c)-('A'-10)) \
275 bytes.back() = (bytes.back() << 4) | v;
282 ByteArray Digest::hexStringToByteArray(std::string_view
str)
284 return hexStrToBArr<ByteArray>( std::move(
str) );
287 UByteArray Digest::hexStringToUByteArray( std::string_view
str )
289 return hexStrToBArr<UByteArray>( std::move(
str) );
293 UByteArray Digest::digestVector()
296 if(!_dp->maybeInit())
301 if(!EVP_DigestFinal_ex(_dp->mdctx.get(), _dp->md_value, &_dp->md_len))
303 _dp->finalized =
true;
305 r.reserve(_dp->md_len);
306 for(
unsigned i = 0; i < _dp->md_len; ++i)
307 r.push_back(_dp->md_value[i]);
311 bool Digest::update(
const char* bytes,
size_t len)
318 if(!_dp->maybeInit())
324 if(!_dp->maybeInit())
328 if(!EVP_DigestUpdate(_dp->mdctx.get(),
reinterpret_cast<const unsigned char*
>(bytes), len))
331 _dp->bytesHashed += len;
335 bool Digest::update(std::istream &is,
size_t bufsize)
345 is.read(buf, bufsize);
346 readed = is.gcount();
347 if(readed && !update(buf, readed))
354 ByteCount Digest::bytesHashed()
const
356 return _dp->bytesHashed;
359 std::string Digest::digest(
const std::string& name, std::istream& is,
size_t bufsize)
361 if(name.empty() || !is)
362 return std::string();
365 if(!digest.create(name))
366 return std::string();
368 if ( !digest.update( is, bufsize ))
369 return std::string();
371 return digest.digest();
374 std::string Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
376 std::istringstream is( input );
377 return digest( name, is, bufsize );
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Store and operate with byte count.
unsigned char md_value[EVP_MAX_MD_SIZE]
static bool openssl_digests_added
const P & operator=(const P &p)=delete
zypp::ByteCount bytesHashed
zypp::shared_ptr< EVP_MD_CTX > EvpDataPtr
static const std::string & md5()
md5
static const std::string & sha384()
sha384
static const std::string & sha512()
sha512
static const std::string & sha1()
sha1
static const std::string & sha256()
sha256
static const std::string & sha224()
sha224
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.