1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Digest.cc
11 * \todo replace by Blocxx
15 #include <cstdio> // snprintf
16 #include <openssl/evp.h>
23 #ifdef DIGEST_TESTSUITE
27 #include "zypp/Digest.h"
31 bool DigestReport::askUserToAcceptNoDigest( const zypp::Pathname &file )
34 bool DigestReport::askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
37 bool DigestReport::askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
41 const std::string & Digest::md5()
42 { static std::string _type( "md5" ); return _type; }
44 const std::string & Digest::sha1()
45 { static std::string _type( "sha1" ); return _type; }
47 const std::string & Digest::sha256()
48 { static std::string _type( "sha256" ); return _type; }
54 const P& operator=(const P& p);
62 unsigned char md_value[EVP_MAX_MD_SIZE];
67 static bool openssl_digests_added;
71 inline bool maybeInit();
72 inline void cleanup();
78 bool Digest::P::openssl_digests_added = false;
92 bool Digest::P::maybeInit()
94 if(!openssl_digests_added)
96 OpenSSL_add_all_digests();
97 openssl_digests_added = true;
102 md = EVP_get_digestbyname(name.c_str());
106 EVP_MD_CTX_init(&mdctx);
108 if(!EVP_DigestInit_ex(&mdctx, md, NULL))
112 ::memset(md_value, 0, sizeof(md_value));
118 void Digest::P::cleanup()
122 EVP_MD_CTX_cleanup(&mdctx);
127 Digest::Digest() : _dp(new P())
136 bool Digest::create(const std::string& name)
138 if(name.empty()) return false;
145 return _dp->maybeInit();
148 const std::string& Digest::name()
153 std::string Digest::digest()
155 if(!_dp->maybeInit())
160 if(!EVP_DigestFinal_ex(&_dp->mdctx, _dp->md_value, &_dp->md_len))
163 _dp->finalized = true;
166 char mdtxt[_dp->md_len*2 + 1];
167 mdtxt[_dp->md_len*2] = '\0';
169 for(unsigned i = 0; i < _dp->md_len; ++i)
171 ::snprintf(mdtxt + i*2, 3, "%02hhx", _dp->md_value[i]);
174 return std::string(mdtxt);
177 bool Digest::update(const char* bytes, size_t len)
184 if(!_dp->maybeInit())
190 if(!_dp->maybeInit())
194 if(!EVP_DigestUpdate(&_dp->mdctx, reinterpret_cast<const unsigned char*>(bytes), len))
200 std::string Digest::digest(const std::string& name, std::istream& is, size_t bufsize)
202 if(name.empty() || !is)
208 if(!digest.create(name))
215 is.read(buf, bufsize);
216 readed = is.gcount();
217 if(readed && !digest.update(buf, readed))
221 return digest.digest();
224 std::string Digest::digest( const std::string & name, const std::string & input, size_t bufsize )
226 istringstream is( input );
227 return digest( name, is, bufsize );
230 #ifdef DIGEST_TESTSUITE
231 int main(int argc, char *argv[])
233 bool openssl = false;
236 if(argc > 1 && string(argv[argpos]) == "--openssl")
242 if(argc - argpos < 2)
244 cerr << "Usage: " << argv[0] << " <DIGESTNAME> <FILE>" << endl;
248 const char* digestname = argv[argpos++];
249 const char* fn = argv[argpos++];
253 string digest = Digest::digest(digestname, file);
256 cout << digestname << "(" << fn << ")= " << digest << endl;
258 cout << digest << " " << fn << endl;