forked from irods/irods
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5392] added additional hash algorithms
see [irods/irods_client_globus_connector#9] sha512 sha1 adler32
- Loading branch information
1 parent
40ad875
commit fa05c3a
Showing
10 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef _ADLER32_STRATEGY_HPP_ | ||
#define _ADLER32_STRATEGY_HPP_ | ||
|
||
#include "HashStrategy.hpp" | ||
#include <string> | ||
#include <openssl/sha.h> | ||
|
||
namespace irods { | ||
const std::string ADLER32_NAME( "adler32" ); | ||
class ADLER32Strategy : public HashStrategy { | ||
public: | ||
ADLER32Strategy() {}; | ||
virtual ~ADLER32Strategy() {}; | ||
|
||
virtual std::string name() const { | ||
return ADLER32_NAME; | ||
} | ||
virtual error init( boost::any& context ) const; | ||
virtual error update( const std::string& data, boost::any& context ) const; | ||
virtual error digest( std::string& messageDigest, boost::any& context ) const; | ||
virtual bool isChecksum( const std::string& ) const; | ||
|
||
}; | ||
}; // namespace irods | ||
|
||
#endif // _ADLER32_STRATEGY_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef _SHA1_STRATEGY_HPP_ | ||
#define _SHA1_STRATEGY_HPP_ | ||
|
||
#include "HashStrategy.hpp" | ||
#include <string> | ||
#include <openssl/sha.h> | ||
|
||
namespace irods { | ||
const std::string SHA1_NAME( "sha1" ); | ||
class SHA1Strategy : public HashStrategy { | ||
public: | ||
SHA1Strategy() {}; | ||
virtual ~SHA1Strategy() {}; | ||
|
||
virtual std::string name() const { | ||
return SHA1_NAME; | ||
} | ||
virtual error init( boost::any& context ) const; | ||
virtual error update( const std::string& data, boost::any& context ) const; | ||
virtual error digest( std::string& messageDigest, boost::any& context ) const; | ||
virtual bool isChecksum( const std::string& ) const; | ||
|
||
}; | ||
}; // namespace irods | ||
|
||
#endif // _SHA1_STRATEGY_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef _SHA512_STRATEGY_HPP_ | ||
#define _SHA512_STRATEGY_HPP_ | ||
|
||
#include "HashStrategy.hpp" | ||
#include <string> | ||
#include <openssl/sha.h> | ||
|
||
namespace irods { | ||
const std::string SHA512_NAME( "sha512" ); | ||
class SHA512Strategy : public HashStrategy { | ||
public: | ||
SHA512Strategy() {}; | ||
virtual ~SHA512Strategy() {}; | ||
|
||
virtual std::string name() const { | ||
return SHA512_NAME; | ||
} | ||
virtual error init( boost::any& context ) const; | ||
virtual error update( const std::string& data, boost::any& context ) const; | ||
virtual error digest( std::string& messageDigest, boost::any& context ) const; | ||
virtual bool isChecksum( const std::string& ) const; | ||
|
||
}; | ||
}; // namespace irods | ||
|
||
#endif // _SHA512_STRATEGY_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "ADLER32Strategy.hpp" | ||
#include "checksum.hpp" | ||
|
||
#include <string> | ||
#include <sstream> | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <string.h> | ||
#include <boost/algorithm/string/predicate.hpp> | ||
//#include <zlib.h> | ||
|
||
#include "base64.h" | ||
|
||
namespace irods { | ||
|
||
struct adler32_parts { | ||
uint32_t a; | ||
uint32_t b; | ||
}; | ||
|
||
adler32_parts adler32_init() { | ||
return adler32_parts{1, 0}; | ||
} | ||
|
||
static adler32_parts adler32_update(const adler32_parts& parts, const unsigned char *data, size_t len) { | ||
|
||
const uint32_t MOD_ADLER = 65521; | ||
|
||
uint32_t a = parts.a, b = parts.b; | ||
|
||
// Process each byte of the data in order | ||
for (size_t index = 0; index < len; ++index) | ||
{ | ||
a = (a + data[index]) % MOD_ADLER; | ||
b = (b + a) % MOD_ADLER; | ||
} | ||
|
||
return adler32_parts{a, b}; | ||
} | ||
|
||
static uint32_t adler32_final(const adler32_parts& parts) { | ||
return (parts.b << 16) | parts.a; | ||
} | ||
|
||
|
||
error | ||
ADLER32Strategy::init( boost::any& _context ) const { | ||
_context = adler32_init(); | ||
return SUCCESS(); | ||
} | ||
|
||
error | ||
ADLER32Strategy::update( const std::string& data, boost::any& _context ) const { | ||
|
||
_context = adler32_update(boost::any_cast<adler32_parts>(_context), reinterpret_cast<const unsigned char*>(data.c_str()), data.size()); | ||
return SUCCESS(); | ||
} | ||
|
||
error | ||
ADLER32Strategy::digest( std::string& _messageDigest, boost::any& _context ) const { | ||
|
||
const unsigned int ADLER32_DIGEST_LENGTH = 4; | ||
|
||
uint32_t result = adler32_final(boost::any_cast<adler32_parts>(_context)); | ||
|
||
std::stringstream ss; | ||
ss << std::setfill('0') << std::hex << std::setw(ADLER32_DIGEST_LENGTH * 2) << result; | ||
|
||
_messageDigest = ADLER32_CHKSUM_PREFIX; | ||
_messageDigest += ss.str(); | ||
|
||
return SUCCESS(); | ||
} | ||
|
||
bool | ||
ADLER32Strategy::isChecksum( const std::string& _chksum ) const { | ||
return boost::starts_with( _chksum, ADLER32_CHKSUM_PREFIX ); | ||
} | ||
}; // namespace irods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "SHA1Strategy.hpp" | ||
#include "checksum.hpp" | ||
|
||
#include <string> | ||
#include <sstream> | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <string.h> | ||
#include <boost/algorithm/string/predicate.hpp> | ||
#include <openssl/sha.h> | ||
|
||
#include "base64.h" | ||
|
||
namespace irods { | ||
|
||
error | ||
SHA1Strategy::init( boost::any& _context ) const { | ||
_context = SHA_CTX(); | ||
SHA1_Init( boost::any_cast<SHA_CTX>( &_context ) ); | ||
return SUCCESS(); | ||
} | ||
|
||
error | ||
SHA1Strategy::update( const std::string& data, boost::any& _context ) const { | ||
SHA1_Update( boost::any_cast<SHA_CTX>( &_context ), data.c_str(), data.size() ); | ||
return SUCCESS(); | ||
} | ||
|
||
error | ||
SHA1Strategy::digest( std::string& _messageDigest, boost::any& _context ) const { | ||
unsigned char final_buffer[SHA_DIGEST_LENGTH]; | ||
SHA1_Final( final_buffer, boost::any_cast<SHA_CTX>( &_context ) ); | ||
int len = strlen( SHA1_CHKSUM_PREFIX ); | ||
unsigned long out_len = CHKSUM_LEN - len; | ||
|
||
unsigned char out_buffer[CHKSUM_LEN]; | ||
base64_encode( final_buffer, SHA_DIGEST_LENGTH, out_buffer, &out_len ); | ||
|
||
_messageDigest = SHA1_CHKSUM_PREFIX; | ||
_messageDigest += std::string( ( char* )out_buffer, out_len ); | ||
|
||
return SUCCESS(); | ||
} | ||
|
||
bool | ||
SHA1Strategy::isChecksum( const std::string& _chksum ) const { | ||
return boost::starts_with( _chksum, SHA1_CHKSUM_PREFIX ); | ||
} | ||
}; // namespace irods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "SHA512Strategy.hpp" | ||
#include "checksum.hpp" | ||
|
||
#include <string> | ||
#include <sstream> | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <string.h> | ||
#include <boost/algorithm/string/predicate.hpp> | ||
#include <openssl/sha.h> | ||
|
||
#include "base64.h" | ||
|
||
namespace irods { | ||
|
||
error | ||
SHA512Strategy::init( boost::any& _context ) const { | ||
_context = SHA512_CTX(); | ||
SHA512_Init( boost::any_cast<SHA512_CTX>( &_context ) ); | ||
return SUCCESS(); | ||
} | ||
|
||
error | ||
SHA512Strategy::update( const std::string& data, boost::any& _context ) const { | ||
SHA512_Update( boost::any_cast<SHA512_CTX>( &_context ), data.c_str(), data.size() ); | ||
return SUCCESS(); | ||
} | ||
|
||
error | ||
SHA512Strategy::digest( std::string& _messageDigest, boost::any& _context ) const { | ||
unsigned char final_buffer[SHA512_DIGEST_LENGTH]; | ||
SHA512_Final( final_buffer, boost::any_cast<SHA512_CTX>( &_context ) ); | ||
int len = strlen( SHA512_CHKSUM_PREFIX ); | ||
unsigned long out_len = CHKSUM_LEN * 2 - len; | ||
|
||
unsigned char out_buffer[CHKSUM_LEN * 2]; | ||
base64_encode( final_buffer, SHA512_DIGEST_LENGTH, out_buffer, &out_len ); | ||
|
||
_messageDigest = SHA512_CHKSUM_PREFIX; | ||
_messageDigest += std::string( ( char* )out_buffer, out_len ); | ||
|
||
return SUCCESS(); | ||
} | ||
|
||
bool | ||
SHA512Strategy::isChecksum( const std::string& _chksum ) const { | ||
return boost::starts_with( _chksum, SHA512_CHKSUM_PREFIX ); | ||
} | ||
}; // namespace irods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters