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; }
93 if(!openssl_digests_added)
96 ENGINE_load_builtin_engines();
97 ENGINE_register_all_complete();
98 OpenSSL_add_all_digests();
99 openssl_digests_added =
true;
104 md = EVP_get_digestbyname(
name.c_str());
108 #if OPENSSL_VERSION_NUMBER < 0x10100000L 109 EvpDataPtr tmp_mdctx(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
111 EvpDataPtr tmp_mdctx(EVP_MD_CTX_new(), EVP_MD_CTX_free);
116 if (!EVP_DigestInit_ex(tmp_mdctx.get(), md, NULL)) {
121 ::memset(md_value, 0,
sizeof(md_value));
123 mdctx.swap(tmp_mdctx);
145 if(
name.empty())
return false;
183 return std::string();
185 std::vector<char> resData ( vec.size()*2 + 1,
'\0' );
186 char *mdtxt = &resData[0];
187 for(
unsigned i = 0; i < vec.size(); ++i)
189 ::snprintf( mdtxt+(i*2), 3,
"%02hhx", vec[i]);
191 return std::string( resData.data() );
194 #ifdef __cpp_lib_string_view 196 template <
typename BArr>
197 BArr hexStrToBArr ( std::string_view &&
str ) {
201 #define c2h(c) (((c)>='0' && (c)<='9') ? ((c)-'0') \ 202 : ((c)>='a' && (c)<='f') ? ((c)-('a'-10)) \ 203 : ((c)>='A' && (c)<='F') ? ((c)-('A'-10)) \ 212 bytes.back() = (bytes.back() << 4) | v;
219 ByteArray Digest::hexStringToByteArray(std::string_view
str)
221 return hexStrToBArr<ByteArray>( std::move(
str) );
224 UByteArray Digest::hexStringToUByteArray( std::string_view
str )
226 return hexStrToBArr<UByteArray>( std::move(
str) );
243 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
265 if(!EVP_DigestUpdate(
_dp->
mdctx.get(),
reinterpret_cast<const unsigned char*
>(bytes), len))
281 is.read(buf, bufsize);
282 readed = is.gcount();
283 if(readed && !
update(buf, readed))
290 std::string
Digest::digest(
const std::string& name, std::istream& is,
size_t bufsize)
292 if(
name.empty() || !is)
293 return std::string();
297 return std::string();
299 if ( !
digest.update( is, bufsize ))
300 return std::string();
305 std::string
Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
307 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
String related utilities and Regular expression matching.
const std::string & name()
get the name of the current digest algorithm
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]
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