1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Digest.h
11 * \todo replace by Blocxx
15 #ifndef ZYPP_MEDIA_DIGEST_H
16 #define ZYPP_MEDIA_DIGEST_H
22 #include "zypp/Callback.h"
23 #include "zypp/Pathname.h"
28 struct DigestReport : public callback::ReportBase
30 virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file );
31 virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name );
32 virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found );
37 /** \brief Compute Message Digests (MD5, SHA1 etc)
39 * The computation works by initializing the algorithm using create(). This
40 * will construct an internal state. successive calls to update() deliver the
41 * data for which the digest is to be computed. After all data has been
42 * deliverd, a call to digest() finalizes the computation and returns the
52 Digest(const Digest& d);
54 const Digest& operator=(const Digest& d);
57 /** \name Well known digest algorithm names. */
60 static const std::string & md5();
62 static const std::string & sha1();
64 static const std::string & sha256();
71 /** \brief initialize creation of a new message digest
73 * Since openssl is used as backend you may use anything that openssl
74 * supports (see man 1 dgst). Common examples are md5 or sha1. sha1
75 * should be preferred when creating digests to verify the authenticity
78 * successive calls to this funcion will destroy the internal state and
81 * @param name name of the message digest algorithm.
82 * @return whether an error occured
84 bool create(const std::string& name);
86 /** \brief get the name of the current digest algorithm */
87 const std::string& name();
89 /** \brief feed data into digest computation algorithm
92 * @return whether an error occured
94 bool update(const char* bytes, size_t len);
96 /** \brief get hex string representation of the digest
98 * this function will finalize the digest computation. calls to update
99 * after this function will start from scratch
101 * @return hex string representation of the digest
103 std::string digest();
105 /** \brief get vector of unsigned char representation of the digest
107 * this function will finalize the digest computation. calls to update
108 * after this function will start from scratch
110 * @return vector representation of the digest
112 std::vector<unsigned char> digestVector();
114 /** \brief reset internal digest state
116 * this function is equivalent to calling create() with an unchanged name,
117 * but it may be implemented in a more efficient way.
121 /** \brief compute digest of a stream. convenience function
123 * calls create, update and digest in one function. The data for the
124 * computation is read from the stream
126 * @param name name of the digest algorithm, \see create
127 * @param is an input stream to get the data from
128 * @param bufsize size of the buffer used for update(). Be careful, this is on the stack.
129 * @return the digest or empty on error
131 static std::string digest(const std::string& name, std::istream& is, size_t bufsize = 4096);
133 /** \overload Reading input data from \c string. */
134 static std::string digest( const std::string & name, const std::string & input, size_t bufsize = 4096 );