Imported Upstream version 14.37.1
[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 #include <vector>
21
22 #include "zypp/Callback.h"
23 #include "zypp/Pathname.h"
24
25 namespace zypp {
26
27
28   struct DigestReport : public callback::ReportBase
29   {
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 );
33   };
34
35
36
37     /** \brief Compute Message Digests (MD5, SHA1 etc)
38      *
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
43      * result
44      * */
45     class Digest
46     {
47       private:
48         class P;
49         P* _dp;
50
51         // disabled
52         Digest(const Digest& d);
53         // disabled
54         const Digest& operator=(const Digest& d);
55
56       public:
57         /** \name Well known digest algorithm names. */
58         //@{
59         /** md5 */
60         static const std::string & md5();
61         /** sha1 */
62         static const std::string & sha1();
63         /** sha224 */
64         static const std::string & sha224();
65         /** sha256 */
66         static const std::string & sha256();
67         /** sha384 */
68         static const std::string & sha384();
69         /** sha512 */
70         static const std::string & sha512();
71         //@}
72
73       public:
74         Digest();
75         ~Digest();
76
77         /** \brief initialize creation of a new message digest
78          *
79          * Since openssl is used as backend you may use anything that openssl
80          * supports (see man 1 dgst). Common examples are md5 or sha1. sha1
81          * should be preferred when creating digests to verify the authenticity
82          * of something.
83          *
84          * successive calls to this funcion will destroy the internal state and
85          * reinit from scratch
86          *
87          * @param name name of the message digest algorithm.
88          * @return whether an error occured
89          * */
90         bool create(const std::string& name);
91
92         /** \brief get the name of the current digest algorithm */
93         const std::string& name();
94
95         /** \brief feed data into digest computation algorithm
96          * @param bytes
97          * @param len
98          * @return whether an error occured
99          * */
100         bool update(const char* bytes, size_t len);
101
102         /** \brief get hex string representation of the digest
103          *
104          * this function will finalize the digest computation. calls to update
105          * after this function will start from scratch
106          *
107          * @return hex string representation of the digest
108          * */
109         std::string digest();
110
111         /** \brief get vector of unsigned char representation of the digest
112          *
113          * this function will finalize the digest computation. calls to update
114          * after this function will start from scratch
115          *
116          * @return vector representation of the digest
117          * */
118         std::vector<unsigned char> digestVector();
119
120         /** \brief reset internal digest state
121          *
122          * this function is equivalent to calling create() with an unchanged name,
123          * but it may be implemented in a more efficient way.
124          */
125         bool reset();
126
127         /** \brief compute digest of a stream. convenience function
128          *
129          * calls create, update and digest in one function. The data for the
130          * computation is read from the stream
131          *
132          * @param name name of the digest algorithm, \see create
133          * @param is an input stream to get the data from
134          * @param bufsize size of the buffer used for update(). Be careful, this is on the stack.
135          * @return the digest or empty on error
136          * */
137         static std::string digest(const std::string& name, std::istream& is, size_t bufsize = 4096);
138
139         /** \overload Reading input data from \c string. */
140         static std::string digest( const std::string & name, const std::string & input, size_t bufsize = 4096 );
141     };
142
143 } // namespace zypp
144
145 #endif