Skip to content

Commit

Permalink
add support for SHA224/384/512
Browse files Browse the repository at this point in the history
  • Loading branch information
mlandres committed Mar 16, 2015
1 parent fa6d1e9 commit 60475d0
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 8 deletions.
36 changes: 32 additions & 4 deletions tests/zypp/CheckSum_test.cc
@@ -1,4 +1,3 @@

#include <iostream>
#include <list>
#include <string>
Expand All @@ -9,17 +8,46 @@
#include "zypp/base/Logger.h"
#include "zypp/base/Exception.h"
#include "zypp/ZYppFactory.h"
#include "zypp/Digest.h"
#include "zypp/ZYpp.h"


using boost::unit_test::test_case;
using namespace std;
using namespace zypp;

void chksumtest( const std::string & type_r, const std::string & sum_r )
{
BOOST_CHECK_EQUAL( type_r, CheckSum( sum_r ).type() ); // autodetect type
BOOST_CHECK_EQUAL( type_r, CheckSum( type_r, sum_r ).type() );
BOOST_CHECK_EQUAL( sum_r, Digest::digest( type_r, "" ) );
for ( const std::string & t : { "md5", "sha1", "sha224", "sha256", "sha384", "sha512", } )
{
if ( t != type_r )
{
BOOST_CHECK_THROW( CheckSum( t, sum_r ), Exception ); // wrong type/size
}
}
}

// most frequently you implement test cases as a free functions
BOOST_AUTO_TEST_CASE(checksum_test)
{
BOOST_CHECK_THROW( CheckSum( "sha1", "dsdsads" ), Exception ); // wrong size
BOOST_CHECK_THROW( CheckSum( "sha256", "dsdsads" ), Exception ); // wrong size
BOOST_CHECK_THROW( CheckSum( "md5", "dsdsads" ), Exception ); // wrong size
CheckSum e;
BOOST_CHECK( e.empty() );
BOOST_CHECK( e.type().empty() );
BOOST_CHECK( e.checksum().empty() );
// sum for ""
// md5 32 d41d8cd98f00b204e9800998ecf8427e
// 1 40 da39a3ee5e6b4b0d3255bfef95601890afd80709
// 224 56 d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f
// 256 64 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
// 384 96 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b
// 512 128 cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
chksumtest( CheckSum::md5Type(), "d41d8cd98f00b204e9800998ecf8427e" );
chksumtest( CheckSum::sha1Type(), "da39a3ee5e6b4b0d3255bfef95601890afd80709" );
chksumtest( CheckSum::sha224Type(), "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f" );
chksumtest( CheckSum::sha256Type(), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" );
chksumtest( CheckSum::sha384Type(), "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" );
chksumtest( CheckSum::sha512Type(), "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" );
}
52 changes: 48 additions & 4 deletions zypp/CheckSum.cc
Expand Up @@ -26,24 +26,54 @@ namespace zypp
{ /////////////////////////////////////////////////////////////////

const std::string & CheckSum::md5Type()
{ static std::string _type( "md5" ); return _type; }
{ return Digest::md5(); }

const std::string & CheckSum::shaType()
{ static std::string _type( "sha" ); return _type; }

const std::string & CheckSum::sha1Type()
{ static std::string _type( "sha1" ); return _type; }
{ return Digest::sha1(); }

const std::string & CheckSum::sha224Type()
{ return Digest::sha224(); }

const std::string & CheckSum::sha256Type()
{ static std::string _type( "sha256" ); return _type; }
{ 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;
Expand All @@ -55,6 +85,17 @@ namespace zypp
// 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;
Expand Down Expand Up @@ -97,7 +138,10 @@ namespace zypp
if ( _type == md5Type()
|| _type == shaType()
|| _type == sha1Type()
|| _type == sha256Type() )
|| _type == sha224Type()
|| _type == sha256Type()
|| _type == sha384Type()
|| _type == sha512Type() )
{
ZYPP_THROW( CheckSumException( msg ) );
}
Expand Down
15 changes: 15 additions & 0 deletions zypp/CheckSum.h
Expand Up @@ -65,27 +65,39 @@ namespace zypp
static const std::string & md5Type();
static const std::string & shaType();
static const std::string & sha1Type();
static const std::string & sha224Type();
static const std::string & sha256Type();
static const std::string & sha384Type();
static const std::string & sha512Type();

/** \name Creates a checksum for algorithm \param type. */
//@{
static CheckSum md5( const std::string & checksum ) { return CheckSum( md5Type(), checksum); }
static CheckSum sha( const std::string & checksum ) { return CheckSum( shaType(), checksum); }
static CheckSum sha1( const std::string & checksum ) { return CheckSum( sha1Type(), checksum); }
static CheckSum sha224( const std::string & checksum ) { return CheckSum( sha224Type(), checksum); }
static CheckSum sha256( const std::string & checksum ) { return CheckSum( sha256Type(), checksum); }
static CheckSum sha384( const std::string & checksum ) { return CheckSum( sha384Type(), checksum); }
static CheckSum sha512( const std::string & checksum ) { return CheckSum( sha512Type(), checksum); }
//@}

/** \name Reads the content of \param input_r and computes the checksum. */
//@{
static CheckSum md5( std::istream & input_r ) { return CheckSum( md5Type(), input_r ); }
static CheckSum sha( std::istream & input_r ) { return CheckSum( sha1Type(), input_r ); }
static CheckSum sha1( std::istream & input_r ) { return CheckSum( sha1Type(), input_r ); }
static CheckSum sha224( std::istream & input_r ) { return CheckSum( sha224Type(), input_r ); }
static CheckSum sha256( std::istream & input_r ) { return CheckSum( sha256Type(), input_r ); }
static CheckSum sha384( std::istream & input_r ) { return CheckSum( sha384Type(), input_r ); }
static CheckSum sha512( std::istream & input_r ) { return CheckSum( sha512Type(), input_r ); }
#ifndef SWIG // Swig treats it as syntax error
static CheckSum md5( std::istream && input_r ) { return CheckSum( md5Type(), input_r ); }
static CheckSum sha( std::istream && input_r ) { return CheckSum( sha1Type(), input_r ); }
static CheckSum sha1( std::istream && input_r ) { return CheckSum( sha1Type(), input_r ); }
static CheckSum sha224( std::istream && input_r ) { return CheckSum( sha224Type(), input_r ); }
static CheckSum sha256( std::istream && input_r ) { return CheckSum( sha256Type(), input_r ); }
static CheckSum sha384( std::istream && input_r ) { return CheckSum( sha384Type(), input_r ); }
static CheckSum sha512( std::istream && input_r ) { return CheckSum( sha512Type(), input_r ); }
#endif
//@}

Expand All @@ -94,7 +106,10 @@ namespace zypp
static CheckSum md5FromString( const std::string & input_r ) { return md5( std::stringstream( input_r ) ); }
static CheckSum shaFromString( const std::string & input_r ) { return sha( std::stringstream( input_r ) ); }
static CheckSum sha1FromString( const std::string & input_r ) { return sha1( std::stringstream( input_r ) ); }
static CheckSum sha224FromString( const std::string & input_r ) { return sha224( std::stringstream( input_r ) ); }
static CheckSum sha256FromString( const std::string & input_r ) { return sha256( std::stringstream( input_r ) ); }
static CheckSum sha384FromString( const std::string & input_r ) { return sha384( std::stringstream( input_r ) ); }
static CheckSum sha512FromString( const std::string & input_r ) { return sha512( std::stringstream( input_r ) ); }
//@}

public:
Expand Down
9 changes: 9 additions & 0 deletions zypp/Digest.cc
Expand Up @@ -46,9 +46,18 @@ namespace zypp {
const std::string & Digest::sha1()
{ static std::string _type( "sha1" ); return _type; }

const std::string & Digest::sha224()
{ static std::string _type( "sha224" ); return _type; }

const std::string & Digest::sha256()
{ static std::string _type( "sha256" ); return _type; }

const std::string & Digest::sha384()
{ static std::string _type( "sha384" ); return _type; }

const std::string & Digest::sha512()
{ static std::string _type( "sha512" ); return _type; }

// private data
class Digest::P
{
Expand Down
6 changes: 6 additions & 0 deletions zypp/Digest.h
Expand Up @@ -60,8 +60,14 @@ namespace zypp {
static const std::string & md5();
/** sha1 */
static const std::string & sha1();
/** sha224 */
static const std::string & sha224();
/** sha256 */
static const std::string & sha256();
/** sha384 */
static const std::string & sha384();
/** sha512 */
static const std::string & sha512();
//@}

public:
Expand Down
12 changes: 12 additions & 0 deletions zypp/sat/LookupAttr.cc
Expand Up @@ -695,9 +695,21 @@ namespace zypp
return CheckSum::sha1( ::repodata_chk2str( _dip->data, solvAttrType(), (unsigned char *)_dip->kv.str ) );
break;

case REPOKEY_TYPE_SHA224:
return CheckSum::sha224( ::repodata_chk2str( _dip->data, solvAttrType(), (unsigned char *)_dip->kv.str ) );
break;

case REPOKEY_TYPE_SHA256:
return CheckSum::sha256( ::repodata_chk2str( _dip->data, solvAttrType(), (unsigned char *)_dip->kv.str ) );
break;

case REPOKEY_TYPE_SHA384:
return CheckSum::sha384( ::repodata_chk2str( _dip->data, solvAttrType(), (unsigned char *)_dip->kv.str ) );
break;

case REPOKEY_TYPE_SHA512:
return CheckSum::sha512( ::repodata_chk2str( _dip->data, solvAttrType(), (unsigned char *)_dip->kv.str ) );
break;
}
}
return CheckSum();
Expand Down
3 changes: 3 additions & 0 deletions zypp/sat/Solvable.cc
Expand Up @@ -196,7 +196,10 @@ namespace zypp
{
case REPOKEY_TYPE_MD5: return CheckSum::md5( s );
case REPOKEY_TYPE_SHA1: return CheckSum::sha1( s );
case REPOKEY_TYPE_SHA224: return CheckSum::sha224( s );
case REPOKEY_TYPE_SHA256: return CheckSum::sha256( s );
case REPOKEY_TYPE_SHA384: return CheckSum::sha384( s );
case REPOKEY_TYPE_SHA512: return CheckSum::sha512( s );
}
return CheckSum( std::string(), s ); // try to autodetect
}
Expand Down

0 comments on commit 60475d0

Please sign in to comment.