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; }
71 #if OPENSSL_API_LEVEL >= 30000 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 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);
171 #if OPENSSL_API_LEVEL >= 30000 188 _dp = std::move( other._dp );
197 if(
name.empty())
return false;
204 return _dp->maybeInit();
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;
231 if ( !
_dp->name.empty () )
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 ) {
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) );
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]);
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;
343 is.read(buf, bufsize);
344 readed = is.gcount();
345 if(readed && !
update(buf, readed))
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();
364 return std::string();
366 if ( !
digest.update( is, bufsize ))
367 return std::string();
372 std::string
Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
374 std::istringstream is( input );
UByteArray digestVector()
get vector of unsigned char representation of the digest
static const std::string & sha256()
sha256
static const std::string & sha1()
sha1
std::string digest()
get hex string representation of the digest
Compute Message Digests (MD5, SHA1 etc)
zypp::shared_ptr< EVP_MD_CTX > EvpDataPtr
Store and operate with byte count.
String related utilities and Regular expression matching.
const std::string & name()
get the name of the current digest algorithm
zypp::ByteCount bytesHashed
const Digest & operator=(const Digest &d)=delete
bool reset()
reset internal digest state
static std::string digestVectorToString(const UByteArray &vec)
get hex string representation of the digest vector given as parameter
static const std::string & sha512()
sha512
unsigned char md_value[EVP_MAX_MD_SIZE]
zypp::ByteCount bytesHashed() const
Returns the number of input bytes that have been added to the hash.
static bool openssl_digests_added
bool create(const std::string &name)
initialize creation of a new message digest
static const std::string & md5()
md5
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
static const std::string & sha224()
sha224
Digest clone() const
Returns a clone of the current Digest and returns it.
Easy-to use interface to the ZYPP dependency resolver.
bool update(const char *bytes, size_t len)
feed data into digest computation algorithm
const P & operator=(const P &p)
static const std::string & sha384()
sha384