Remove obsolete ResStatus bits.
[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         /** \name Well known digest algorithm names. */
57         //@{
58         /** md5 */
59         static const std::string & md5();
60         /** sha1 */
61         static const std::string & sha1();
62         /** sha256 */
63         static const std::string & sha256();
64         //@}
65
66       public:
67         Digest();
68         ~Digest();
69
70         /** \brief initialize creation of a new message digest
71          *
72          * Since openssl is used as backend you may use anything that openssl
73          * supports (see man 1 dgst). Common examples are md5 or sha1. sha1
74          * should be preferred when creating digests to verify the authenticity
75          * of something.
76          *
77          * successive calls to this funcion will destroy the internal state and
78          * reinit from scratch
79          *
80          * @param name name of the message digest algorithm.
81          * @return whether an error occured
82          * */
83         bool create(const std::string& name);
84
85         /** \brief get the name of the current digest algorithm */
86         const std::string& name();
87
88         /** \brief feed data into digest computation algorithm
89          * @param bytes
90          * @param len
91          * @return whether an error occured
92          * */
93         bool update(const char* bytes, size_t len);
94
95         /** \brief get hex string representation of the digest
96          *
97          * this function will finalize the digest computation. calls to update
98          * after this function will start from scratch
99          *
100          * @return hex string representation of the digest
101          * */
102         std::string digest();
103
104         /** \brief compute digest of a stream. convenience function
105          *
106          * calls create, update and digest in one function. The data for the
107          * computation is read from the stream
108          *
109          * @param name name of the digest algorithm, \see create
110          * @param is an input stream to get the data from
111          * @param bufsize size of the buffer used for update(). Be careful, this is on the stack.
112          * @return the digest or empty on error
113          * */
114         static std::string digest(const std::string& name, std::istream& is, size_t bufsize = 4096);
115
116         /** \overload Reading input data from \c string. */
117         static std::string digest( const std::string & name, const std::string & input, size_t bufsize = 4096 );
118     };
119
120 } // namespace zypp
121
122 #endif