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