libzypp: zypp-core/CheckSum.cc Source File

libzypp 17.38.5

CheckSum.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <sstream>
14
15#include <zypp-core/base/Logger.h>
16#include <zypp-core/base/Gettext.h>
17#include <zypp-core/base/String.h>
18
19#include <zypp-core/CheckSum.h>
20#include <zypp-core/Digest.h>
21
22using std::endl;
23
25namespace zypp
26{
27
28 const std::string & CheckSum::md5Type()
29 { return Digest::md5(); }
30
31 const std::string & CheckSum::shaType()
32 { static std::string _type( "sha" ); return _type; }
33
34 const std::string & CheckSum::sha1Type()
35 { return Digest::sha1(); }
36
37 const std::string & CheckSum::sha224Type()
38 { return Digest::sha224(); }
39
40 const std::string & CheckSum::sha256Type()
41 { return Digest::sha256(); }
42
43 const std::string & CheckSum::sha384Type()
44 { return Digest::sha384(); }
45
46 const std::string & CheckSum::sha512Type()
47 { return Digest::sha512(); }
48
49 CheckSum::CheckSum( const std::string & type, const std::string & checksum )
50 : _type( str::toLower( type ) )
51 , _checksum( checksum )
52 {
53 switch ( checksum.size() )
54 {
55 case 128:
56 if ( _type == sha512Type() )
57 return;
58 if ( _type.empty() || _type == shaType() )
59 {
60 _type = sha512Type();
61 return;
62 }
63 // else: dubious
64 break;
65
66 case 96:
67 if ( _type == sha384Type() )
68 return;
69 if ( _type.empty() || _type == shaType() )
70 {
71 _type = sha384Type();
72 return;
73 }
74 // else: dubious
75 break;
76
77 case 64:
78 if ( _type == sha256Type() )
79 return;
80 if ( _type.empty() || _type == shaType() )
81 {
82 _type = sha256Type();
83 return;
84 }
85 // else: dubious
86 break;
87
88 case 56:
89 if ( _type == sha224Type() )
90 return;
91 if ( _type.empty() || _type == shaType() )
92 {
93 _type = sha224Type();
94 return;
95 }
96 // else: dubious
97 break;
98
99 case 40:
100 if ( _type == sha1Type() )
101 return;
102 if ( _type.empty() || _type == shaType() )
103 {
104 _type = sha1Type();
105 return;
106 }
107 // else: dubious
108 break;
109
110 case 32:
111 if ( _type == md5Type() )
112 return;
113 if ( _type.empty() )
114 {
115 _type = md5Type();
116 return;
117 }
118 // else: dubious
119 break;
120
121 case 0:
122 return; // empty checksum is ok
123 break;
124
125 default:
126 if ( _type.empty() )
127 {
128 WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
129 return;
130 }
131 // else: dubious
132 break;
133 }
134
135 // dubious: Throw on malformed known types, otherwise log a warning.
136 std::string msg = str::form ( _("Dubious type '%s' for %u byte checksum '%s'"),
137 _type.c_str(), checksum.size(), _checksum.c_str() );
138 if ( _type == md5Type()
139 || _type == shaType()
140 || _type == sha1Type()
141 || _type == sha224Type()
142 || _type == sha256Type()
143 || _type == sha384Type()
144 || _type == sha512Type() )
145 {
146 ZYPP_THROW( CheckSumException( msg ) );
147 }
148 else
149 {
150 WAR << msg << endl;
151 }
152 }
153
154 CheckSum::CheckSum( const std::string & type_r, std::istream & input_r )
155 {
156 if ( input_r.good() && ! type_r.empty() )
157 {
158 _type = str::toLower( type_r );
159 _checksum = Digest::digest( _type, input_r );
160 if ( ! input_r.eof() || _checksum.empty() )
161 {
162 _type = _checksum = std::string();
163 }
164 }
165 }
166
167 std::string CheckSum::type() const
168 { return _type; }
169
170 std::string CheckSum::checksum() const
171 { return _checksum; }
172
173 bool CheckSum::empty() const
174 { return (checksum().empty() || type().empty()); }
175
176 std::string CheckSum::asString() const
177 {
178 std::ostringstream str;
179 str << *this;
180 return str.str();
181 }
182
183 std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
184 {
185 if ( obj.checksum().empty() )
186 {
187 return str << std::string("NoCheckSum");
188 }
189
190 return str << ( obj.type().empty() ? std::string("UNKNOWN") : obj.type() ) << '-' << obj.checksum();
191 }
192
193 std::ostream & dumpAsXmlOn( std::ostream & str, const CheckSum & obj )
194 {
195 const std::string & type( obj.type() );
196 const std::string & checksum( obj.checksum() );
197 str << "<checksum";
198 if ( ! type.empty() ) str << " type=\"" << type << "\"";
199 if ( checksum.empty() )
200 str << "/>";
201 else
202 str << ">" << checksum << "</checksum>";
203 return str;
204 }
205
207 bool operator==( const CheckSum & lhs, const CheckSum & rhs )
208 { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); }
209
211 bool operator!=( const CheckSum & lhs, const CheckSum & rhs )
212 { return ! ( lhs == rhs ); }
213
215} // namespace zypp
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
Gettext.h
_
#define _(MSG)
Definition Gettext.h:39
Logger.h
WAR
#define WAR
Definition Logger.h:101
operator<<
std::ostream & operator<<(std::ostream &str, const zypp::sat::detail::CDataiterator *obj)
Definition LookupAttr.cc:808
String.h
zypp::CheckSum::sha512Type
static const std::string & sha512Type()
Definition CheckSum.cc:46
zypp::CheckSum::_type
std::string _type
Definition CheckSum.h:121
zypp::CheckSum::empty
bool empty() const
Definition CheckSum.cc:173
zypp::CheckSum::operator==
bool operator==(const CheckSum &lhs, const CheckSum &rhs)
Definition CheckSum.cc:207
zypp::CheckSum::asString
std::string asString() const
Definition CheckSum.cc:176
zypp::CheckSum::sha256Type
static const std::string & sha256Type()
Definition CheckSum.cc:40
zypp::CheckSum::sha384Type
static const std::string & sha384Type()
Definition CheckSum.cc:43
zypp::CheckSum::dumpAsXmlOn
std::ostream & dumpAsXmlOn(std::ostream &str, const CheckSum &obj)
XML output.
Definition CheckSum.cc:193
zypp::CheckSum::shaType
static const std::string & shaType()
Definition CheckSum.cc:31
zypp::CheckSum::sha1Type
static const std::string & sha1Type()
Definition CheckSum.cc:34
zypp::CheckSum::sha224Type
static const std::string & sha224Type()
Definition CheckSum.cc:37
zypp::CheckSum::md5Type
static const std::string & md5Type()
Definition CheckSum.cc:28
zypp::CheckSum::type
std::string type() const
Definition CheckSum.cc:167
zypp::CheckSum::checksum
std::string checksum() const
Definition CheckSum.cc:170
zypp::CheckSum::CheckSum
CheckSum()
Default Ctor: empty checksum.
Definition CheckSum.h:37
zypp::CheckSum::operator!=
bool operator!=(const CheckSum &lhs, const CheckSum &rhs)
Definition CheckSum.cc:211
zypp::CheckSum::_checksum
std::string _checksum
Definition CheckSum.h:122
zypp::Digest::md5
static const std::string & md5()
md5
Definition Digest.cc:42
zypp::Digest::sha384
static const std::string & sha384()
sha384
Definition Digest.cc:54
zypp::Digest::digest
std::string digest()
get hex string representation of the digest
Definition Digest.cc:239
zypp::Digest::sha512
static const std::string & sha512()
sha512
Definition Digest.cc:57
zypp::Digest::sha1
static const std::string & sha1()
sha1
Definition Digest.cc:45
zypp::Digest::sha256
static const std::string & sha256()
sha256
Definition Digest.cc:51
zypp::Digest::sha224
static const std::string & sha224()
sha224
Definition Digest.cc:48
str
String related utilities and Regular expression matching.
zypp::str::toLower
std::string toLower(const std::string &s)
Return lowercase version of s.
Definition String.cc:180
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::CheckSumException
Definition CheckSum.h:27
CheckSum.h
Digest.h