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> 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 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 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);
173 #if OPENSSL_API_LEVEL >= 30000 190 _dp = std::move( other._dp );
199 if(
name.empty())
return false;
206 return _dp->maybeInit();
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;
233 if ( !
_dp->name.empty () )
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 ) {
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) );
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]);
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;
345 is.read(buf, bufsize);
346 readed = is.gcount();
347 if(readed && !
update(buf, readed))
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();
366 return std::string();
368 if ( !
digest.update( is, bufsize ))
369 return std::string();
374 std::string
Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
376 std::istringstream is( input );
zypp::shared_ptr< EVP_MD_CTX > EvpDataPtr
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)
Store and operate with byte count.
const P & operator=(const P &p)=delete
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
zypp::UByteArray UByteArray
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
static const std::string & sha384()
sha384
zypp::ByteArray ByteArray