- new callbacks for failing digest
[platform/upstream/libzypp.git] / zypp / Digest.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp//Digest.h
10  *
11  * \todo replace by Blocxx
12  *
13 */
14
15 #ifndef ZYPP_MEDIA_DIGEST_H
16 #define ZYPP_MEDIA_DIGEST_H
17
18 #include <string>
19 #include <iosfwd>
20
21 #include <zypp/Callback.h>
22 #include <zypp/Pathname.h>
23
24 namespace zypp {
25
26
27   struct DigestReport : public callback::ReportBase
28   {
29     virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
30     { return true; }
31     virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
32     { return true; }
33     virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
34     { return true; }
35   };
36   
37
38
39     /** \brief Compute Message Digests (MD5, SHA1 etc)
40      *
41      * The computation works by initializing the algorithm using create(). This
42      * will construct an internal state. successive calls to update() deliver the
43      * data for which the digest is to be computed. After all data has been
44      * deliverd, a call to digest() finalizes the computation and returns the
45      * result
46      * */
47     class Digest
48     {
49       private:
50         class P;
51         P* _dp;
52     
53         // disabled
54         Digest(const Digest& d);
55         // disabled
56         const Digest& operator=(const Digest& d);
57     
58       public:
59         Digest();
60         ~Digest();
61     
62         /** \brief initialize creation of a new message digest
63          * 
64          * Since openssl is used as backend you may use anything that openssl
65          * supports (see man 1 dgst). Common examples are md5 or sha1. sha1
66          * should be preferred when creating digests to verify the authenticity
67          * of something.
68          *
69          * successive calls to this funcion will destroy the internal state and
70          * reinit from scratch
71          *
72          * @param name name of the message digest algorithm.
73          * @return whether an error occured
74          * */
75         bool create(const std::string& name);
76     
77         /** \brief get the name of the current digest algorithm */
78         const std::string& name();
79     
80         /** \brief feed data into digest computation algorithm
81          * @param bytes
82          * @param len
83          * @return whether an error occured
84          * */
85         bool update(const char* bytes, size_t len);
86     
87         /** \brief get hex string representation of the digest
88          *
89          * this function will finalize the digest computation. calls to update
90          * after this function will start from scratch
91          *
92          * @return hex string representation of the digest
93          * */
94         std::string digest();
95         
96         /** \brief compute digest of a stream. convenience function
97          *
98          * calls create, update and digest in one function. The data for the
99          * computation is read from the stream
100          *
101          * @param name name of the digest algorithm, \see create
102          * @param is an input stream to get the data from
103          * @param bufsize size of the buffer used for update(). Be careful, this is on the stack.
104          * @return the digest or empty on error
105          * */
106         static std::string digest(const std::string& name, std::istream& is, size_t bufsize = 4096);
107     };
108     
109 } // namespace zypp
110
111 #endif