1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/CheckSum.cc
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/String.h"
16 #include "zypp/CheckSum.h"
17 #include "zypp/Digest.h"
21 ///////////////////////////////////////////////////////////////////
23 { /////////////////////////////////////////////////////////////////
25 const std::string & CheckSum::md5Type()
26 { static std::string _type( "md5" ); return _type; }
28 const std::string & CheckSum::shaType()
29 { static std::string _type( "sha" ); return _type; }
31 const std::string & CheckSum::sha1Type()
32 { static std::string _type( "sha1" ); return _type; }
34 const std::string & CheckSum::sha256Type()
35 { static std::string _type( "sha256" ); return _type; }
41 CheckSum::CheckSum( const std::string & type, const std::string & checksum )
42 : _type( str::toLower( type ) )
43 , _checksum( checksum )
45 switch ( checksum.size() )
48 if ( _type == sha256Type() )
50 if ( _type.empty() || _type == shaType() )
59 if ( _type == sha1Type() )
61 if ( _type.empty() || _type == shaType() )
70 if ( _type == md5Type() )
81 return; // empty checksum is ok
87 WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
95 WAR << "Dubious type '" << _type << "' for " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
98 CheckSum::CheckSum( const std::string & type_r, std::istream & input_r )
100 if ( input_r.good() && ! type_r.empty() )
102 _type = str::toLower( type_r );
103 _checksum = Digest::digest( _type, input_r );
104 if ( ! input_r.eof() || _checksum.empty() )
106 _type = _checksum = std::string();
111 std::string CheckSum::type() const
114 std::string CheckSum::checksum() const
115 { return _checksum; }
117 bool CheckSum::empty() const
118 { return (checksum().empty() || type().empty()); }
120 std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
122 if ( obj.checksum().empty() )
124 return str << std::string("NoCheckSum");
127 return str << ( obj.type().empty() ? std::string("UNKNOWN") : obj.type() ) << '-' << obj.checksum();
130 /** \relates CheckSum */
131 bool operator==( const CheckSum & lhs, const CheckSum & rhs )
132 { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); }
134 /** \relates CheckSum */
135 bool operator!=( const CheckSum & lhs, const CheckSum & rhs )
136 { return ! ( lhs == rhs ); }
138 /////////////////////////////////////////////////////////////////
140 ///////////////////////////////////////////////////////////////////