Merge pull request #23 from openSUSE/drop_package_manager
[platform/upstream/libzypp.git] / zypp / CheckSum.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/CheckSum.h
10  *
11 */
12 #ifndef ZYPP_CHECKSUM_H
13 #define ZYPP_CHECKSUM_H
14
15 #include <iosfwd>
16 #include <string>
17
18 #include "zypp/base/Exception.h"
19 #include "zypp/Pathname.h"
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24
25   struct CheckSumException : public Exception
26   {
27     CheckSumException( const std::string & msg )
28       : Exception( msg )
29     {}
30   };
31
32   class CheckSum
33   {
34   public:
35     /** Default Ctor: empty checksum. */
36     CheckSum();
37     /**
38      * Creates a checksum auto probing the algorithm type.
39      * \throws CheckSumException if the checksum is invalid and can't be constructed
40      */
41     CheckSum( const std::string & checksum );
42     /**
43      * Creates a checksum for algorithm \param type.
44      * \throws CheckSumException if the checksum is invalid and can't be constructed
45      */
46     CheckSum( const std::string & type, const std::string & checksum );
47     /**
48      * Reads the content of \param input_r and computes the checksum.
49      */
50     CheckSum( const std::string & type, std::istream & input_r );
51
52 #ifndef SWIG // Swig treats it as syntax error
53       /** Move Ctor */
54       CheckSum( const std::string & type, std::istream && input_r )
55       : CheckSum( type, input_r )
56       {}
57 #endif
58
59   public:
60     static const std::string & md5Type();
61     static const std::string & shaType();
62     static const std::string & sha1Type();
63     static const std::string & sha256Type();
64
65     static CheckSum md5( const std::string & checksum )
66     { return  CheckSum( md5Type(), checksum); }
67     static CheckSum sha( const std::string & checksum )
68     { return  CheckSum( shaType(), checksum); }
69     static CheckSum sha1( const std::string & checksum )
70     { return  CheckSum( sha1Type(), checksum); }
71     static CheckSum sha256( const std::string & checksum )
72     { return  CheckSum( sha256Type(), checksum); }
73
74     static CheckSum md5( std::istream & input_r )
75     { return  CheckSum( md5Type(), input_r ); }
76     static CheckSum sha( std::istream & input_r )
77     { return  CheckSum( sha1Type(), input_r ); }
78     static CheckSum sha1( std::istream & input_r )
79     { return  CheckSum( sha1Type(), input_r ); }
80     static CheckSum sha256( std::istream & input_r )
81     { return  CheckSum( sha256Type(), input_r ); }
82
83   public:
84     std::string type() const;
85     std::string checksum() const;
86     bool empty() const;
87
88   public:
89     std::string asString() const;
90
91   private:
92     std::string _type;
93     std::string _checksum;
94   };
95
96   /** \relates CheckSum Stream output. */
97   std::ostream & operator<<( std::ostream & str, const CheckSum & obj );
98
99   /** \relates CheckSum */
100   bool operator==( const CheckSum & lhs, const CheckSum & rhs );
101
102   /** \relates CheckSum */
103   bool operator!=( const CheckSum & lhs, const CheckSum & rhs );
104
105 } // namespace zypp
106 ///////////////////////////////////////////////////////////////////
107 #endif // ZYPP_CHECKSUM_H