00001
00002
00003
00004
00005
00006
00007
00008
00013 #include "zypp/base/Logger.h"
00014 #include "zypp/base/String.h"
00015
00016 #include "zypp/CheckSum.h"
00017 #include "zypp/Digest.h"
00018
00019 using std::endl;
00020
00022 namespace zypp
00023 {
00024
00025 const std::string & CheckSum::md5Type()
00026 { static std::string _type( "md5" ); return _type; }
00027
00028 const std::string & CheckSum::shaType()
00029 { static std::string _type( "sha" ); return _type; }
00030
00031 const std::string & CheckSum::sha1Type()
00032 { static std::string _type( "sha1" ); return _type; }
00033
00034 const std::string & CheckSum::sha256Type()
00035 { static std::string _type( "sha256" ); return _type; }
00036
00037
00038 CheckSum::CheckSum()
00039 {}
00040
00041 CheckSum::CheckSum( const std::string & type, const std::string & checksum )
00042 : _type( str::toLower( type ) )
00043 , _checksum( checksum )
00044 {
00045 switch ( checksum.size() )
00046 {
00047 case 64:
00048 if ( _type == sha256Type() )
00049 return;
00050 if ( _type.empty() || _type == shaType() )
00051 {
00052 _type = sha256Type();
00053 return;
00054 }
00055
00056 break;
00057
00058 case 40:
00059 if ( _type == sha1Type() )
00060 return;
00061 if ( _type.empty() || _type == shaType() )
00062 {
00063 _type = sha1Type();
00064 return;
00065 }
00066
00067 break;
00068
00069 case 32:
00070 if ( _type == md5Type() )
00071 return;
00072 if ( _type.empty() )
00073 {
00074 _type = md5Type();
00075 return;
00076 }
00077
00078 break;
00079
00080 case 0:
00081 return;
00082 break;
00083
00084 default:
00085 if ( _type.empty() )
00086 {
00087 WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
00088 return;
00089 }
00090
00091 break;
00092 }
00093
00094
00095 WAR << "Dubious type '" << _type << "' for " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
00096 }
00097
00098 CheckSum::CheckSum( const std::string & type_r, std::istream & input_r )
00099 {
00100 if ( input_r.good() && ! type_r.empty() )
00101 {
00102 _type = str::toLower( type_r );
00103 _checksum = Digest::digest( _type, input_r );
00104 if ( ! input_r.eof() || _checksum.empty() )
00105 {
00106 _type = _checksum = std::string();
00107 }
00108 }
00109 }
00110
00111 std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
00112 {
00113 if ( obj.checksum().empty() )
00114 {
00115 return str << std::string("NoCheckSum");
00116 }
00117
00118 return str << ( obj.type().empty() ? std::string("UNKNOWN") : obj.type() ) << '-' << obj.checksum();
00119 }
00120
00121
00123 }