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 #if OPENSSL_VERSION_NUMBER < 0x10100000L
121 mdctx = (EVP_MD_CTX*) malloc(
sizeof(EVP_MD_CTX));
122 EVP_MD_CTX_init(mdctx);
124 mdctx = EVP_MD_CTX_new();
126 if(!EVP_DigestInit_ex(mdctx, md, NULL))
130 ::memset(md_value, 0,
sizeof(md_value));
140 #if OPENSSL_VERSION_NUMBER < 0x10100000L
141 EVP_MD_CTX_cleanup(mdctx);
143 EVP_MD_CTX_free(mdctx);
161 if(name.empty())
return false;
194 return std::string();
199 return std::string();
207 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
209 ::snprintf(mdtxt + i*2, 3,
"%02hhx",
_dp->
md_value[i]);
212 return std::string(mdtxt);
217 std::vector<unsigned char> r;
228 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
250 if(!EVP_DigestUpdate(
_dp->
mdctx, reinterpret_cast<const unsigned char*>(bytes), len))
256 std::string
Digest::digest(
const std::string& name, std::istream& is,
size_t bufsize)
258 if(name.empty() || !is)
271 is.read(buf, bufsize);
272 readed = is.gcount();
273 if(readed && !digest.
update(buf, readed))
280 std::string
Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
282 istringstream is( input );
283 return digest( name, is, bufsize );
286 #ifdef DIGEST_TESTSUITE
287 int main(
int argc,
char *argv[])
289 bool openssl =
false;
292 if(argc > 1 &&
string(argv[argpos]) ==
"--openssl")
298 if(argc - argpos < 2)
300 cerr <<
"Usage: " << argv[0] <<
" <DIGESTNAME> <FILE>" << endl;
304 const char* digestname = argv[argpos++];
305 const char* fn = argv[argpos++];
312 cout << digestname <<
"(" << fn <<
")= " << digest << endl;
314 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