16 #include <openssl/evp.h> 17 #include <openssl/conf.h> 18 #include <openssl/engine.h> 25 #include <zypp-core/Digest.h> 26 #include <zypp-core/base/PtrTypes.h> 33 {
static std::string _type(
"md5" );
return _type; }
36 {
static std::string _type(
"sha1" );
return _type; }
39 {
static std::string _type(
"sha224" );
return _type; }
42 {
static std::string _type(
"sha256" );
return _type; }
45 {
static std::string _type(
"sha384" );
return _type; }
48 {
static std::string _type(
"sha512" );
return _type; }
94 if(!openssl_digests_added)
97 ENGINE_load_builtin_engines();
98 ENGINE_register_all_complete();
99 OpenSSL_add_all_digests();
100 openssl_digests_added =
true;
105 md = EVP_get_digestbyname(
name.c_str());
109 #if OPENSSL_VERSION_NUMBER < 0x10100000L 110 EvpDataPtr tmp_mdctx(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
112 EvpDataPtr tmp_mdctx(EVP_MD_CTX_new(), EVP_MD_CTX_free);
117 if (!EVP_DigestInit_ex(tmp_mdctx.get(), md, NULL)) {
122 ::memset(md_value, 0,
sizeof(md_value));
126 mdctx.swap(tmp_mdctx);
148 if(
name.empty())
return false;
187 return std::string();
189 std::vector<char> resData ( vec.size()*2 + 1,
'\0' );
190 char *mdtxt = &resData[0];
191 for(
unsigned i = 0; i < vec.size(); ++i)
193 ::snprintf( mdtxt+(i*2), 3,
"%02hhx", vec[i]);
195 return std::string( resData.data() );
198 #ifdef __cpp_lib_string_view 200 template <
typename BArr>
201 BArr hexStrToBArr ( std::string_view &&
str ) {
205 #define c2h(c) (((c)>='0' && (c)<='9') ? ((c)-'0') \ 206 : ((c)>='a' && (c)<='f') ? ((c)-('a'-10)) \ 207 : ((c)>='A' && (c)<='F') ? ((c)-('A'-10)) \ 216 bytes.back() = (bytes.back() << 4) | v;
223 ByteArray Digest::hexStringToByteArray(std::string_view
str)
225 return hexStrToBArr<ByteArray>( std::move(
str) );
228 UByteArray Digest::hexStringToUByteArray( std::string_view
str )
230 return hexStrToBArr<UByteArray>( std::move(
str) );
247 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
269 if(!EVP_DigestUpdate(
_dp->
mdctx.get(),
reinterpret_cast<const unsigned char*
>(bytes), len))
286 is.read(buf, bufsize);
287 readed = is.gcount();
288 if(readed && !
update(buf, readed))
300 std::string
Digest::digest(
const std::string& name, std::istream& is,
size_t bufsize)
302 if(
name.empty() || !is)
303 return std::string();
307 return std::string();
309 if ( !
digest.update( is, bufsize ))
310 return std::string();
315 std::string
Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
317 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
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
static const std::string & sha224()
sha224
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