void generateHash( const char* filename ) { // Create hasher Hasher hasher; MD5Strategy* md5Strategy = new MD5Strategy(); hasher.addStrategy( md5Strategy ); SHA256Strategy* sha256Strategy = new SHA256Strategy(); hasher.addStrategy( sha256Strategy ); // Read the file and hash it ifstream input( filename, ios_base::in | ios_base::binary ); if ( input.is_open() ) { char buffer[1024]; hasher.init(); while ( !input.eof() ) { input.read( buffer, 1023 ); int numRead = input.gcount(); hasher.update( buffer, numRead ); } input.close(); string messageDigest; hasher.digest( "MD5", messageDigest ); cout << messageDigest << " "; hasher.digest( "SHA256", messageDigest ); cout << messageDigest << endl; } else { cerr << "ERROR: Unable to open file: " << filename << endl; } }
error getHasher( const std::string& _name, Hasher& _hasher ) { boost::unordered_map<const std::string, const HashStrategy*>::const_iterator it = _strategies.find( _name ); if ( _strategies.end() == it ) { std::stringstream msg; msg << "Unknown hashing scheme [" << _name << "]"; return ERROR( SYS_INVALID_INPUT_PARAM, msg.str() ); } _hasher.init( it->second ); return SUCCESS(); }