16 #include <openssl/evp.h>
17 #include <openssl/conf.h>
18 #include <openssl/engine.h>
25 #ifdef DIGEST_TESTSUITE
45 {
static std::string _type(
"md5" );
return _type; }
48 {
static std::string _type(
"sha1" );
return _type; }
51 {
static std::string _type(
"sha224" );
return _type; }
54 {
static std::string _type(
"sha256" );
return _type; }
57 {
static std::string _type(
"sha384" );
return _type; }
60 {
static std::string _type(
"sha512" );
return _type; }
106 if(!openssl_digests_added)
108 OPENSSL_config(NULL);
109 ENGINE_load_builtin_engines();
110 ENGINE_register_all_complete();
111 OpenSSL_add_all_digests();
112 openssl_digests_added =
true;
117 md = EVP_get_digestbyname(
name.c_str());
121 #if OPENSSL_VERSION_NUMBER < 0x10100000L
122 EvpDataPtr tmp_mdctx(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
124 EvpDataPtr tmp_mdctx(EVP_MD_CTX_new(), EVP_MD_CTX_free);
129 if (!EVP_DigestInit_ex(tmp_mdctx.get(), md, NULL)) {
134 ::memset(md_value, 0,
sizeof(md_value));
136 mdctx.swap(tmp_mdctx);
158 if(name.empty())
return false;
191 return std::string();
196 return std::string();
204 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
206 ::snprintf(mdtxt + i*2, 3,
"%02hhx",
_dp->
md_value[i]);
209 return std::string(mdtxt);
214 std::vector<unsigned char> r;
225 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
247 if(!EVP_DigestUpdate(
_dp->
mdctx.get(),
reinterpret_cast<const unsigned char*
>(bytes), len))
253 std::string
Digest::digest(
const std::string& name, std::istream& is,
size_t bufsize)
255 if(name.empty() || !is)
268 is.read(buf, bufsize);
269 readed = is.gcount();
270 if(readed && !digest.
update(buf, readed))
277 std::string
Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
279 istringstream is( input );
280 return digest( name, is, bufsize );
283 #ifdef DIGEST_TESTSUITE
284 int main(
int argc,
char *argv[])
286 bool openssl =
false;
289 if(argc > 1 &&
string(argv[argpos]) ==
"--openssl")
295 if(argc - argpos < 2)
297 cerr <<
"Usage: " << argv[0] <<
" <DIGESTNAME> <FILE>" << endl;
301 const char* digestname = argv[argpos++];
302 const char* fn = argv[argpos++];
309 cout << digestname <<
"(" << fn <<
")= " << digest << endl;
311 cout << digest <<
" " << fn << endl;
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
virtual bool askUserToAccepUnknownDigest(const Pathname &file, const std::string &name)
const std::string & name()
get the name of the current digest algorithm
bool reset()
reset internal digest state
static const std::string & sha512()
sha512
std::vector< unsigned char > digestVector()
get vector of unsigned char representation of the digest
unsigned char md_value[EVP_MAX_MD_SIZE]
virtual bool askUserToAcceptNoDigest(const zypp::Pathname &file)
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
virtual bool askUserToAcceptWrongDigest(const Pathname &file, const std::string &requested, const std::string &found)
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