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(
"sha224" );
return _type; }
53 {
static std::string _type(
"sha256" );
return _type; }
56 {
static std::string _type(
"sha384" );
return _type; }
59 {
static std::string _type(
"sha512" );
return _type; }
105 if(!openssl_digests_added)
107 OPENSSL_config(NULL);
108 ENGINE_load_builtin_engines();
109 ENGINE_register_all_complete();
110 OpenSSL_add_all_digests();
111 openssl_digests_added =
true;
116 md = EVP_get_digestbyname(
name.c_str());
120 EVP_MD_CTX_init(&mdctx);
122 if(!EVP_DigestInit_ex(&mdctx, md, NULL))
126 ::memset(md_value, 0,
sizeof(md_value));
136 EVP_MD_CTX_cleanup(&mdctx);
153 if(name.empty())
return false;
186 return std::string();
191 return std::string();
199 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
201 ::snprintf(mdtxt + i*2, 3,
"%02hhx",
_dp->
md_value[i]);
204 return std::string(mdtxt);
209 std::vector<unsigned char> r;
220 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
242 if(!EVP_DigestUpdate(&
_dp->
mdctx, reinterpret_cast<const unsigned char*>(bytes), len))
248 std::string
Digest::digest(
const std::string& name, std::istream& is,
size_t bufsize)
250 if(name.empty() || !is)
263 is.read(buf, bufsize);
264 readed = is.gcount();
265 if(readed && !digest.
update(buf, readed))
272 std::string
Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
274 istringstream is( input );
275 return digest( name, is, bufsize );
278 #ifdef DIGEST_TESTSUITE
279 int main(
int argc,
char *argv[])
281 bool openssl =
false;
284 if(argc > 1 &&
string(argv[argpos]) ==
"--openssl")
290 if(argc - argpos < 2)
292 cerr <<
"Usage: " << argv[0] <<
" <DIGESTNAME> <FILE>" << endl;
296 const char* digestname = argv[argpos++];
297 const char* fn = argv[argpos++];
304 cout << digestname <<
"(" << fn <<
")= " << digest << endl;
306 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
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