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;
196 return std::string();
198 std::vector<char> resData ( vec.size()*2 + 1,
'\0' );
199 char *mdtxt = &resData[0];
200 for(
unsigned i = 0; i < vec.size(); ++i)
202 ::snprintf( mdtxt+(i*2), 3,
"%02hhx", vec[i]);
204 return std::string( resData.data() );
209 std::vector<unsigned char> r;
220 for(
unsigned i = 0; i <
_dp->
md_len; ++i)
242 if(!EVP_DigestUpdate(
_dp->
mdctx.get(),
reinterpret_cast<const unsigned char*
>(bytes), len))
258 is.read(buf, bufsize);
259 readed = is.gcount();
260 if(readed && !
update(buf, readed))
267 std::string
Digest::digest(
const std::string& name, std::istream& is,
size_t bufsize)
269 if(
name.empty() || !is)
276 if ( !
digest.update( is, bufsize ))
282 std::string
Digest::digest(
const std::string & name,
const std::string & input,
size_t bufsize )
284 istringstream is( input );
288 #ifdef DIGEST_TESTSUITE 289 int main(
int argc,
char *argv[])
291 bool openssl =
false;
294 if(argc > 1 &&
string(argv[argpos]) ==
"--openssl")
300 if(argc - argpos < 2)
302 cerr <<
"Usage: " << argv[0] <<
" <DIGESTNAME> <FILE>" << endl;
306 const char* digestname = argv[argpos++];
307 const char* fn = argv[argpos++];
314 cout << digestname <<
"(" << fn <<
")= " << digest << endl;
316 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)
static std::string digestVectorToString(const std::vector< unsigned char > &vec)
get hex string representation of the digest vector given as parameter
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
Easy-to use interface to the ZYPP dependency resolver.
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