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; }
66 typedef zypp::shared_ptr<EVP_MD_CTX>
EvpDataPtr;
71#if OPENSSL_API_LEVEL >= 30000
72 AutoDispose<EVP_MD *>
md;
76 unsigned char md_value[EVP_MAX_MD_SIZE];
104 bool Digest::P::maybeInit()
106 if(!openssl_digests_added)
108#if OPENSSL_API_LEVEL >= 30000
111 OPENSSL_init_crypto( OPENSSL_INIT_LOAD_CONFIG,
nullptr );
114 if ( !OSSL_PROVIDER_load(
nullptr,
"legacy" ) ) {
115 ERR <<
"Failed to load legacy openssl provider" << std::endl;
117 if ( !OSSL_PROVIDER_load(
nullptr,
"default") ) {
118 ERR <<
"Failed to load default openssl provider" << std::endl;
121 OPENSSL_init_crypto( OPENSSL_INIT_ADD_ALL_DIGESTS,
nullptr );
123# if OPENSSL_API_LEVEL >= 10100
124 OPENSSL_init_crypto( OPENSSL_INIT_LOAD_CONFIG,
nullptr );
126 OPENSSL_config(NULL);
128 ENGINE_load_builtin_engines();
129 ENGINE_register_all_complete();
130 OpenSSL_add_all_digests();
132 openssl_digests_added =
true;
137#if OPENSSL_API_LEVEL >= 30000
140 md = AutoDispose<EVP_MD *>( EVP_MD_fetch (
nullptr, name.c_str(),
nullptr), EVP_MD_free );
142 md = EVP_get_digestbyname(name.c_str());
147#if OPENSSL_VERSION_NUMBER < 0x10100000L
148 EvpDataPtr tmp_mdctx(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
150 EvpDataPtr tmp_mdctx(EVP_MD_CTX_new(), EVP_MD_CTX_free);
155 if (!EVP_DigestInit_ex(tmp_mdctx.get(), md, NULL)) {
160 ::memset(md_value, 0,
sizeof(md_value));
164 mdctx.swap(tmp_mdctx);
169 void Digest::P::cleanup()
171#if OPENSSL_API_LEVEL >= 30000
178 Digest::Digest() : _dp(
std::make_unique<P>() )
182 Digest::Digest(Digest &&other) : _dp(
std::move(other._dp) )
186 Digest &Digest::operator=(Digest &&other)
188 _dp = std::move( other._dp );
195 bool Digest::create(
const std::string& name)
197 if(name.empty())
return false;
204 return _dp->maybeInit();
207 const std::string& Digest::name()
218 (void)EVP_DigestFinal_ex(_dp->mdctx.get(), _dp->md_value, &_dp->md_len);
219 _dp->finalized =
true;
221 if(!EVP_DigestInit_ex(_dp->mdctx.get(), _dp->md, NULL))
223 _dp->finalized =
false;
224 _dp->bytesHashed = 0;
228 Digest Digest::clone()
const
231 if ( !_dp->name.empty () )
232 d.create ( _dp->name );
236 std::string Digest::digest()
238 return digestVectorToString( digestVector() );
241 std::string Digest::digestVectorToString(
const UByteArray &vec)
244 return std::string();
246 std::vector<char> resData ( vec.size()*2 + 1,
'\0' );
247 char *mdtxt = &resData[0];
248 for(
unsigned i = 0; i < vec.size(); ++i)
250 ::snprintf( mdtxt+(i*2), 3,
"%02hhx", vec[i]);
252 return std::string( resData.data() );
255#ifdef __cpp_lib_string_view
257 template <
typename BArr>
258 BArr hexStrToBArr ( std::string_view &&
str ) {
260 for ( std::string::size_type i = 0; i <
str.length(); i+=2 )
262 #define c2h(c) (((c)>='0' && (c)<='9') ? ((c)-'0') \
263 : ((c)>='a' && (c)<='f') ? ((c)-('a'-10)) \
264 : ((c)>='A' && (c)<='F') ? ((c)-('A'-10)) \
273 bytes.back() = (bytes.back() << 4) | v;
280 ByteArray Digest::hexStringToByteArray(std::string_view
str)
282 return hexStrToBArr<ByteArray>( std::move(
str) );
285 UByteArray Digest::hexStringToUByteArray( std::string_view
str )
287 return hexStrToBArr<UByteArray>( std::move(
str) );
291 UByteArray Digest::digestVector()
294 if(!_dp->maybeInit())
299 if(!EVP_DigestFinal_ex(_dp->mdctx.get(), _dp->md_value, &_dp->md_len))
301 _dp->finalized =
true;
303 r.reserve(_dp->md_len);
304 for(
unsigned i = 0; i < _dp->md_len; ++i)
305 r.push_back(_dp->md_value[i]);
309 bool Digest::update(
const char* bytes,
size_t len)
316 if(!_dp->maybeInit())
322 if(!_dp->maybeInit())
326 if(!EVP_DigestUpdate(_dp->mdctx.get(),
reinterpret_cast<const unsigned char*
>(bytes), len))
329 _dp->bytesHashed += len;
333 bool Digest::update(std::istream &is,
size_t bufsize)
343 is.read(buf, bufsize);
344 readed = is.gcount();
345 if(readed && !update(buf, readed))
352 ByteCount Digest::bytesHashed()
const
354 return _dp->bytesHashed;
357 std::string Digest::digest(
const std::string& name, std::istream& is,
size_t bufsize)
359 if(name.empty() || !is)
360 return std::string();
363 if(!digest.create(name))
364 return std::string();
366 if ( !digest.update( is, bufsize ))
367 return std::string();
369 return digest.digest();
372 std::string Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
374 std::istringstream is( input );
375 return digest( name, is, bufsize );
Store and operate with byte count.
const P & operator=(const P &p)
unsigned char md_value[EVP_MAX_MD_SIZE]
static bool openssl_digests_added
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.