- Create the cache directly from the schema (installed) file.
[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     virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name );
31     virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found );
32   };
33   
34
35
36     /** \brief Compute Message Digests (MD5, SHA1 etc)
37      *
38      * The computation works by initializing the algorithm using create(). This
39      * will construct an internal state. successive calls to update() deliver the
40      * data for which the digest is to be computed. After all data has been
41      * deliverd, a call to digest() finalizes the computation and returns the
42      * result
43      * */
44     class Digest
45     {
46       private:
47         class P;
48         P* _dp;
49     
50         // disabled
51         Digest(const Digest& d);
52         // disabled
53         const Digest& operator=(const Digest& d);
54     
55       public:
56         Digest();
57         ~Digest();
58     
59         /** \brief initialize creation of a new message digest
60          * 
61          * Since openssl is used as backend you may use anything that openssl
62          * supports (see man 1 dgst). Common examples are md5 or sha1. sha1
63          * should be preferred when creating digests to verify the authenticity
64          * of something.
65          *
66          * successive calls to this funcion will destroy the internal state and
67          * reinit from scratch
68          *
69          * @param name name of the message digest algorithm.
70          * @return whether an error occured
71          * */
72         bool create(const std::string& name);
73     
74         /** \brief get the name of the current digest algorithm */
75         const std::string& name();
76     
77         /** \brief feed data into digest computation algorithm
78          * @param bytes
79          * @param len
80          * @return whether an error occured
81          * */
82         bool update(const char* bytes, size_t len);
83     
84         /** \brief get hex string representation of the digest
85          *
86          * this function will finalize the digest computation. calls to update
87          * after this function will start from scratch
88          *
89          * @return hex string representation of the digest
90          * */
91         std::string digest();
92         
93         /** \brief compute digest of a stream. convenience function
94          *
95          * calls create, update and digest in one function. The data for the
96          * computation is read from the stream
97          *
98          * @param name name of the digest algorithm, \see create
99          * @param is an input stream to get the data from
100          * @param bufsize size of the buffer used for update(). Be careful, this is on the stack.
101          * @return the digest or empty on error
102          * */
103         static std::string digest(const std::string& name, std::istream& is, size_t bufsize = 4096);
104     };
105     
106 } // namespace zypp
107
108 #endif