16 #include <openssl/evp.h>
17 #include <openssl/conf.h>
18 #include <openssl/engine.h>
25 #ifdef DIGEST_TESTSUITE
44 {
static std::string _type(
"md5" );
return _type; }
47 {
static std::string _type(
"sha1" );
return _type; }
50 {
static std::string _type(
"sha256" );
return _type; }
96 if(!openssl_digests_added)
99 ENGINE_load_builtin_engines();
100 ENGINE_register_all_complete();
101 OpenSSL_add_all_digests();
102 openssl_digests_added =
true;
107 md = EVP_get_digestbyname(
name.c_str());
111 EVP_MD_CTX_init(&mdctx);
113 if(!EVP_DigestInit_ex(&mdctx, md, NULL))
117 ::memset(md_value, 0,
sizeof(md_value));
127 EVP_MD_CTX_cleanup(&mdctx);
144 if(name.empty())
return false;
177 return std::string();
182 return std::string();
190 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
192 ::snprintf(mdtxt + i*2, 3,
"%02hhx",
_dp->
md_value[i]);
195 return std::string(mdtxt);
200 std::vector<unsigned char> r;
211 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
233 if(!EVP_DigestUpdate(&
_dp->
mdctx, reinterpret_cast<const unsigned char*>(bytes), len))
239 std::string
Digest::digest(
const std::string& name, std::istream& is,
size_t bufsize)
241 if(name.empty() || !is)
254 is.read(buf, bufsize);
255 readed = is.gcount();
256 if(readed && !digest.
update(buf, readed))
263 std::string
Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
265 istringstream is( input );
266 return digest( name, is, bufsize );
269 #ifdef DIGEST_TESTSUITE
270 int main(
int argc,
char *argv[])
272 bool openssl =
false;
275 if(argc > 1 &&
string(argv[argpos]) ==
"--openssl")
281 if(argc - argpos < 2)
283 cerr <<
"Usage: " << argv[0] <<
" <DIGESTNAME> <FILE>" << endl;
287 const char* digestname = argv[argpos++];
288 const char* fn = argv[argpos++];
295 cout << digestname <<
"(" << fn <<
")= " << digest << endl;
297 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)
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
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
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)