1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/CheckSum.cc
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/String.h"
19 #include "zypp/CheckSum.h"
20 #include "zypp/Digest.h"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 const std::string & CheckSum::md5Type()
29 { return Digest::md5(); }
31 const std::string & CheckSum::shaType()
32 { static std::string _type( "sha" ); return _type; }
34 const std::string & CheckSum::sha1Type()
35 { return Digest::sha1(); }
37 const std::string & CheckSum::sha224Type()
38 { return Digest::sha224(); }
40 const std::string & CheckSum::sha256Type()
41 { return Digest::sha256(); }
43 const std::string & CheckSum::sha384Type()
44 { return Digest::sha384(); }
46 const std::string & CheckSum::sha512Type()
47 { return Digest::sha512(); }
49 CheckSum::CheckSum( const std::string & type, const std::string & checksum )
50 : _type( str::toLower( type ) )
51 , _checksum( checksum )
53 switch ( checksum.size() )
56 if ( _type == sha512Type() )
58 if ( _type.empty() || _type == shaType() )
67 if ( _type == sha384Type() )
69 if ( _type.empty() || _type == shaType() )
78 if ( _type == sha256Type() )
80 if ( _type.empty() || _type == shaType() )
89 if ( _type == sha224Type() )
91 if ( _type.empty() || _type == shaType() )
100 if ( _type == sha1Type() )
102 if ( _type.empty() || _type == shaType() )
111 if ( _type == md5Type() )
122 return; // empty checksum is ok
128 WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
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() )
146 ZYPP_THROW( CheckSumException( msg ) );
154 CheckSum::CheckSum( const std::string & type_r, std::istream & input_r )
156 if ( input_r.good() && ! type_r.empty() )
158 _type = str::toLower( type_r );
159 _checksum = Digest::digest( _type, input_r );
160 if ( ! input_r.eof() || _checksum.empty() )
162 _type = _checksum = std::string();
167 std::string CheckSum::type() const
170 std::string CheckSum::checksum() const
171 { return _checksum; }
173 bool CheckSum::empty() const
174 { return (checksum().empty() || type().empty()); }
176 std::string CheckSum::asString() const
178 std::ostringstream str;
183 std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
185 if ( obj.checksum().empty() )
187 return str << std::string("NoCheckSum");
190 return str << ( obj.type().empty() ? std::string("UNKNOWN") : obj.type() ) << '-' << obj.checksum();
193 std::ostream & dumpAsXmlOn( std::ostream & str, const CheckSum & obj )
195 const std::string & type( obj.type() );
196 const std::string & checksum( obj.checksum() );
198 if ( ! type.empty() ) str << " type=\"" << type << "\"";
199 if ( checksum.empty() )
202 str << ">" << checksum << "</checksum>";
206 /** \relates CheckSum */
207 bool operator==( const CheckSum & lhs, const CheckSum & rhs )
208 { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); }
210 /** \relates CheckSum */
211 bool operator!=( const CheckSum & lhs, const CheckSum & rhs )
212 { return ! ( lhs == rhs ); }
214 /////////////////////////////////////////////////////////////////
216 ///////////////////////////////////////////////////////////////////