ignore
[platform/upstream/libzypp.git] / zypp / CheckSum.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/CheckSum.cc
10  *
11 */
12
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/String.h"
15
16 #include "zypp/CheckSum.h"
17 #include "zypp/Digest.h"
18
19 using std::endl;
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24
25   const std::string & CheckSum::md5Type()
26   { static std::string _type( "md5" ); return _type; }
27
28   const std::string & CheckSum::shaType()
29   { static std::string _type( "sha" ); return _type; }
30
31   const std::string & CheckSum::sha1Type()
32   { static std::string _type( "sha1" ); return _type; }
33
34   const std::string & CheckSum::sha256Type()
35   { static std::string _type( "sha256" ); return _type; }
36
37
38   CheckSum::CheckSum()
39   {}
40
41   CheckSum::CheckSum( const std::string & type, const std::string & checksum )
42   : _type( str::toLower( type ) )
43   , _checksum( checksum )
44   {
45     switch ( checksum.size() )
46       {
47       case 64:
48         if ( _type == sha256Type() )
49           return;
50         if ( _type.empty() || _type == shaType() )
51           {
52             _type = sha256Type();
53             return;
54           }
55         // else: dubious
56         break;
57
58       case 40:
59         if ( _type == sha1Type() )
60           return;
61         if ( _type.empty() || _type == shaType() )
62           {
63             _type = sha1Type();
64             return;
65           }
66         // else: dubious
67         break;
68
69       case 32:
70         if (  _type == md5Type() )
71           return;
72         if ( _type.empty() )
73           {
74             _type = md5Type();
75             return;
76           }
77         // else: dubious
78         break;
79
80       case 0:
81         return; // empty checksum is ok
82         break;
83
84       default:
85         if ( _type.empty() )
86           {
87             WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
88             return;
89           }
90         // else: dubious
91         break;
92       }
93
94     // dubious
95     WAR << "Dubious type '" << _type << "' for " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
96   }
97
98   CheckSum::CheckSum( const std::string & type_r, std::istream & input_r )
99   {
100     if ( input_r.good() && ! type_r.empty() )
101       {
102         _type = str::toLower( type_r );
103         _checksum = Digest::digest( _type, input_r );
104         if ( ! input_r.eof() || _checksum.empty() )
105           {
106             _type = _checksum = std::string();
107           }
108       }
109   }
110
111   std::string CheckSum::type() const
112   { return _type; }
113
114   std::string CheckSum::checksum() const
115   { return _checksum; }
116
117   bool CheckSum::empty() const
118   { return (checksum().empty() || type().empty()); }
119   
120   std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
121   {
122     if ( obj.checksum().empty() )
123       {
124         return str << std::string("NoCheckSum");
125       }
126
127     return str << ( obj.type().empty() ? std::string("UNKNOWN") : obj.type() ) << '-' << obj.checksum();
128   }
129
130    /** \relates CheckSum */
131   bool operator==( const CheckSum & lhs, const CheckSum & rhs )
132   { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); }
133
134   /** \relates CheckSum */
135   bool operator!=( const CheckSum & lhs, const CheckSum & rhs )
136   { return ! ( lhs == rhs ); }
137
138   /////////////////////////////////////////////////////////////////
139 } // namespace zypp
140 ///////////////////////////////////////////////////////////////////