17#include <openssl/evp.h>
18#include <openssl/conf.h>
19#if OPENSSL_API_LEVEL < 30000
20#include <openssl/engine.h>
29#include <zypp-core/AutoDispose.h>
30#include <zypp-core/Digest.h>
31#include <zypp-core/base/PtrTypes.h>
38 {
static std::string _type(
"md5" );
return _type; }
41 {
static std::string _type(
"sha1" );
return _type; }
44 {
static std::string _type(
"sha224" );
return _type; }
47 {
static std::string _type(
"sha256" );
return _type; }
50 {
static std::string _type(
"sha384" );
return _type; }
53 {
static std::string _type(
"sha512" );
return _type; }
62 typedef zypp::shared_ptr<EVP_MD_CTX>
EvpDataPtr;
67#if OPENSSL_API_LEVEL >= 30000
68 AutoDispose<EVP_MD *>
md;
72 unsigned char md_value[EVP_MAX_MD_SIZE];
100 bool Digest::P::maybeInit()
102 if(!openssl_digests_added)
104#if OPENSSL_API_LEVEL >= 30000
107 OPENSSL_init_crypto( OPENSSL_INIT_LOAD_CONFIG | OPENSSL_INIT_ADD_ALL_DIGESTS,
nullptr );
109# if OPENSSL_API_LEVEL >= 10100
110 OPENSSL_init_crypto( OPENSSL_INIT_LOAD_CONFIG,
nullptr );
112 OPENSSL_config(NULL);
114 ENGINE_load_builtin_engines();
115 ENGINE_register_all_complete();
116 OpenSSL_add_all_digests();
118 openssl_digests_added =
true;
123#if OPENSSL_API_LEVEL >= 30000
126 md = AutoDispose<EVP_MD *>( EVP_MD_fetch (
nullptr, name.c_str(),
nullptr), EVP_MD_free );
128 md = EVP_get_digestbyname(name.c_str());
133#if OPENSSL_VERSION_NUMBER < 0x10100000L
134 EvpDataPtr tmp_mdctx(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
136 EvpDataPtr tmp_mdctx(EVP_MD_CTX_new(), EVP_MD_CTX_free);
141 if (!EVP_DigestInit_ex(tmp_mdctx.get(), md, NULL)) {
146 ::memset(md_value, 0,
sizeof(md_value));
150 mdctx.swap(tmp_mdctx);
155 void Digest::P::cleanup()
157#if OPENSSL_API_LEVEL >= 30000
164 Digest::Digest() : _dp(new P())
173 bool Digest::create(
const std::string& name)
175 if(name.empty())
return false;
182 return _dp->maybeInit();
185 const std::string& Digest::name()
196 (void)EVP_DigestFinal_ex(_dp->mdctx.get(), _dp->md_value, &_dp->md_len);
197 _dp->finalized =
true;
199 if(!EVP_DigestInit_ex(_dp->mdctx.get(), _dp->md, NULL))
201 _dp->finalized =
false;
202 _dp->bytesHashed = 0;
206 std::string Digest::digest()
208 return digestVectorToString( digestVector() );
211 std::string Digest::digestVectorToString(
const UByteArray &vec)
214 return std::string();
216 std::vector<char> resData ( vec.size()*2 + 1,
'\0' );
217 char *mdtxt = &resData[0];
218 for(
unsigned i = 0; i < vec.size(); ++i)
220 ::snprintf( mdtxt+(i*2), 3,
"%02hhx", vec[i]);
222 return std::string( resData.data() );
225#ifdef __cpp_lib_string_view
227 template <
typename BArr>
228 BArr hexStrToBArr ( std::string_view &&
str ) {
230 for ( std::string::size_type i = 0; i <
str.length(); i+=2 )
232 #define c2h(c) (((c)>='0' && (c)<='9') ? ((c)-'0') \
233 : ((c)>='a' && (c)<='f') ? ((c)-('a'-10)) \
234 : ((c)>='A' && (c)<='F') ? ((c)-('A'-10)) \
243 bytes.back() = (bytes.back() << 4) | v;
250 ByteArray Digest::hexStringToByteArray(std::string_view
str)
252 return hexStrToBArr<ByteArray>( std::move(
str) );
255 UByteArray Digest::hexStringToUByteArray( std::string_view
str )
257 return hexStrToBArr<UByteArray>( std::move(
str) );
261 UByteArray Digest::digestVector()
264 if(!_dp->maybeInit())
269 if(!EVP_DigestFinal_ex(_dp->mdctx.get(), _dp->md_value, &_dp->md_len))
271 _dp->finalized =
true;
273 r.reserve(_dp->md_len);
274 for(
unsigned i = 0; i < _dp->md_len; ++i)
275 r.push_back(_dp->md_value[i]);
279 bool Digest::update(
const char* bytes,
size_t len)
286 if(!_dp->maybeInit())
292 if(!_dp->maybeInit())
296 if(!EVP_DigestUpdate(_dp->mdctx.get(),
reinterpret_cast<const unsigned char*
>(bytes), len))
299 _dp->bytesHashed += len;
303 bool Digest::update(std::istream &is,
size_t bufsize)
313 is.read(buf, bufsize);
314 readed = is.gcount();
315 if(readed && !update(buf, readed))
322 ByteCount Digest::bytesHashed()
const
324 return _dp->bytesHashed;
327 std::string Digest::digest(
const std::string& name, std::istream& is,
size_t bufsize)
329 if(name.empty() || !is)
330 return std::string();
333 if(!digest.create(name))
334 return std::string();
336 if ( !digest.update( is, bufsize ))
337 return std::string();
339 return digest.digest();
342 std::string Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
344 std::istringstream is( input );
345 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.