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 { static std::string _type( "md5" ); return _type; }
31 const std::string & CheckSum::shaType()
32 { static std::string _type( "sha" ); return _type; }
34 const std::string & CheckSum::sha1Type()
35 { static std::string _type( "sha1" ); return _type; }
37 const std::string & CheckSum::sha256Type()
38 { static std::string _type( "sha256" ); return _type; }
44 CheckSum::CheckSum( const std::string & type, const std::string & checksum )
45 : _type( str::toLower( type ) )
46 , _checksum( checksum )
48 switch ( checksum.size() )
51 if ( _type == sha256Type() )
53 if ( _type.empty() || _type == shaType() )
62 if ( _type == sha1Type() )
64 if ( _type.empty() || _type == shaType() )
73 if ( _type == md5Type() )
84 return; // empty checksum is ok
90 WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
97 // dubious: Throw on malformed known types, otherwise log a warning.
98 std::string msg = str::form ( _("Dubious type '%s' for %u byte checksum '%s'"),
99 _type.c_str(), checksum.size(), _checksum.c_str() );
100 if ( _type == md5Type()
101 || _type == shaType()
102 || _type == sha1Type()
103 || _type == sha256Type() )
105 ZYPP_THROW( CheckSumException( msg ) );
113 CheckSum::CheckSum( const std::string & type_r, std::istream & input_r )
115 if ( input_r.good() && ! type_r.empty() )
117 _type = str::toLower( type_r );
118 _checksum = Digest::digest( _type, input_r );
119 if ( ! input_r.eof() || _checksum.empty() )
121 _type = _checksum = std::string();
126 std::string CheckSum::type() const
129 std::string CheckSum::checksum() const
130 { return _checksum; }
132 bool CheckSum::empty() const
133 { return (checksum().empty() || type().empty()); }
135 std::string CheckSum::asString() const
137 std::ostringstream str;
142 std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
144 if ( obj.checksum().empty() )
146 return str << std::string("NoCheckSum");
149 return str << ( obj.type().empty() ? std::string("UNKNOWN") : obj.type() ) << '-' << obj.checksum();
152 /** \relates CheckSum */
153 bool operator==( const CheckSum & lhs, const CheckSum & rhs )
154 { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); }
156 /** \relates CheckSum */
157 bool operator!=( const CheckSum & lhs, const CheckSum & rhs )
158 { return ! ( lhs == rhs ); }
160 /////////////////////////////////////////////////////////////////
162 ///////////////////////////////////////////////////////////////////