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>
43 {
static std::string _type(
"md5" );
return _type; }
46 {
static std::string _type(
"sha1" );
return _type; }
49 {
static std::string _type(
"sha224" );
return _type; }
52 {
static std::string _type(
"sha256" );
return _type; }
55 {
static std::string _type(
"sha384" );
return _type; }
58 {
static std::string _type(
"sha512" );
return _type; }
63 P(
const P& p) =
delete;
74#if OPENSSL_API_LEVEL >= 30000
111#if OPENSSL_API_LEVEL >= 30000
114 OPENSSL_init_crypto( OPENSSL_INIT_LOAD_CONFIG,
nullptr );
117 if ( !OSSL_PROVIDER_load(
nullptr,
"legacy" ) ) {
118 ERR <<
"Failed to load legacy openssl provider" << std::endl;
120 if ( !OSSL_PROVIDER_load(
nullptr,
"default") ) {
121 ERR <<
"Failed to load default openssl provider" << std::endl;
124 OPENSSL_init_crypto( OPENSSL_INIT_ADD_ALL_DIGESTS,
nullptr );
126# if OPENSSL_VERSION_NUMBER >= 0x10100000L
127 OPENSSL_init_crypto( OPENSSL_INIT_LOAD_CONFIG,
nullptr );
129 OPENSSL_config(NULL);
131 ENGINE_load_builtin_engines();
132 ENGINE_register_all_complete();
133 OpenSSL_add_all_digests();
140#if OPENSSL_API_LEVEL >= 30000
145 md = EVP_get_digestbyname(
name.c_str());
150#if OPENSSL_VERSION_NUMBER < 0x10100000L
151 EvpDataPtr tmp_mdctx(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
153 EvpDataPtr tmp_mdctx(EVP_MD_CTX_new(), EVP_MD_CTX_free);
158 if (!EVP_DigestInit_ex(tmp_mdctx.get(),
md, NULL)) {
167 mdctx.swap(tmp_mdctx);
174#if OPENSSL_API_LEVEL >= 30000
191 _dp = std::move( other._dp );
200 if(
name.empty())
return false;
207 return _dp->maybeInit();
221 (void)EVP_DigestFinal_ex(
_dp->mdctx.get(),
_dp->md_value, &
_dp->md_len);
222 _dp->finalized =
true;
224 if(!EVP_DigestInit_ex(
_dp->mdctx.get(),
_dp->md, NULL))
226 _dp->finalized =
false;
227 _dp->bytesHashed = 0;
234 if ( !
_dp->name.empty () )
247 return std::string();
249 std::vector<char> resData ( vec.size()*2 + 1,
'\0' );
250 char *mdtxt = &resData[0];
251 for(
unsigned i = 0; i < vec.size(); ++i)
253 ::snprintf( mdtxt+(i*2), 3,
"%02hhx", vec[i]);
255 return std::string( resData.data() );
258#ifdef __cpp_lib_string_view
259 ByteArray Digest::hexStringToByteArray(std::string_view
str)
261 return str::hexstringToByteArray<ByteArray>( std::move(
str) );
264 UByteArray Digest::hexStringToUByteArray( std::string_view
str )
266 return str::hexstringToByteArray<UByteArray>( std::move(
str) );
273 if(!
_dp->maybeInit())
278 if(!EVP_DigestFinal_ex(
_dp->mdctx.get(),
_dp->md_value, &
_dp->md_len))
280 _dp->finalized =
true;
282 r.reserve(
_dp->md_len);
283 for(
unsigned i = 0; i <
_dp->md_len; ++i)
284 r.push_back(
_dp->md_value[i]);
295 if(!
_dp->maybeInit())
301 if(!
_dp->maybeInit())
305 if(!EVP_DigestUpdate(
_dp->mdctx.get(),
reinterpret_cast<const unsigned char*
>(bytes), len))
308 _dp->bytesHashed += len;
322 is.read(buf, bufsize);
323 readed = is.gcount();
324 if(readed && !
update(buf, readed))
333 return _dp->bytesHashed;
338 if(
name.empty() || !is)
339 return std::string();
343 return std::string();
345 if ( !
digest.update( is, bufsize ))
346 return std::string();
353 std::istringstream is( input );
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
P & operator=(P &&)=delete
static const std::string & md5()
md5
static const std::string & sha384()
sha384
std::string digest()
get hex string representation of the digest
static const std::string & sha512()
sha512
UByteArray digestVector()
get vector of unsigned char representation of the digest
static const std::string & sha1()
sha1
Digest(const Digest &d)=delete
bool update(const char *bytes, size_t len)
feed data into digest computation algorithm
Digest clone() const
Returns a clone of the current Digest and returns it.
zypp::ByteCount bytesHashed() const
Returns the number of input bytes that have been added to the hash.
static const std::string & sha256()
sha256
const Digest & operator=(const Digest &d)=delete
static const std::string & sha224()
sha224
static std::string digestVectorToString(const UByteArray &vec)
get hex string representation of the digest vector given as parameter
bool reset()
reset internal digest state
const std::string & name()
get the name of the current digest algorithm
bool create(const std::string &name)
initialize creation of a new message digest
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.