"Fossies" - the Fresh Open Source Software Archive

Member "irods-4.2.8/lib/hasher/src/checksum.cpp" (14 May 2020, 6551 Bytes) of package /linux/misc/irods-4.2.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "checksum.cpp" see the Fossies "Dox" file reference documentation.

    1 /* md5Checksum.c - checksumming routine on the client side
    2  */
    3 
    4 #include "irods_stacktrace.hpp"
    5 #include "irods_hasher_factory.hpp"
    6 #include "getRodsEnv.h"
    7 #include "irods_log.hpp"
    8 #include "objInfo.h"
    9 #include "SHA256Strategy.hpp"
   10 #include "rodsKeyWdDef.h"
   11 #include "rcMisc.h"
   12 #include "checksum.hpp"
   13 
   14 #include <fstream>
   15 
   16 #define HASH_BUF_SZ (1024*1024)
   17 
   18 int chksumLocFile(
   19     const char*       _file_name,
   20     char*       _checksum,
   21     const char* _hash_scheme ) {
   22     if ( !_file_name ||
   23             !_checksum  ||
   24             !_hash_scheme ) {
   25         rodsLog(
   26             LOG_ERROR,
   27             "chksumLocFile :: null input param - %p %p %p",
   28             _file_name,
   29             _checksum,
   30             _hash_scheme );
   31         return SYS_INVALID_INPUT_PARAM;
   32     }
   33 
   34     // =-=-=-=-=-=-=-
   35     // capture client side configuration
   36     rodsEnv env;
   37     int status = getRodsEnv( &env );
   38     if ( status < 0 ) {
   39         return status;
   40     }
   41 
   42     // =-=-=-=-=-=-=-
   43     // capture the configured scheme if it is valid
   44     std::string env_scheme( irods::SHA256_NAME );
   45     if ( strlen( env.rodsDefaultHashScheme ) > 0 ) {
   46         env_scheme = env.rodsDefaultHashScheme;
   47 
   48     }
   49 
   50     // =-=-=-=-=-=-=-
   51     // capture the configured hash match policy if it is valid
   52     std::string env_policy;
   53     if ( strlen( env.rodsMatchHashPolicy ) > 0 ) {
   54         env_policy = env.rodsMatchHashPolicy;
   55         // =-=-=-=-=-=-=-
   56         // hash scheme keywords are all lowercase
   57         std::transform(
   58             env_scheme.begin(),
   59             env_scheme.end(),
   60             env_scheme.begin(),
   61             ::tolower );
   62     }
   63 
   64     // =-=-=-=-=-=-=-
   65     // capture the incoming scheme if it is valid
   66     std::string hash_scheme;
   67     if ( _hash_scheme &&
   68             strlen( _hash_scheme ) > 0 &&
   69             strlen( _hash_scheme ) < NAME_LEN ) {
   70         hash_scheme = _hash_scheme;
   71         // =-=-=-=-=-=-=-
   72         // hash scheme keywords are all lowercase
   73         std::transform(
   74             hash_scheme.begin(),
   75             hash_scheme.end(),
   76             hash_scheme.begin(),
   77             ::tolower );
   78     }
   79     else {
   80         hash_scheme = env_scheme;
   81 
   82     }
   83 
   84     // =-=-=-=-=-=-=-
   85     // hash scheme keywords are all lowercase
   86     std::transform(
   87         hash_scheme.begin(),
   88         hash_scheme.end(),
   89         hash_scheme.begin(),
   90         ::tolower );
   91     std::transform(
   92         env_scheme.begin(),
   93         env_scheme.end(),
   94         env_scheme.begin(),
   95         ::tolower );
   96 
   97 
   98     // =-=-=-=-=-=-=-
   99     // verify checksum scheme against configuration
  100     // if requested
  101     std::string final_scheme( env_scheme );
  102     if ( !hash_scheme.empty() ) {
  103         if ( !env_policy.empty() ) {
  104             if ( irods::STRICT_HASH_POLICY == env_policy ) {
  105                 if ( env_scheme != hash_scheme ) {
  106                     return USER_HASH_TYPE_MISMATCH;
  107                 }
  108             }
  109         }
  110 
  111         final_scheme = hash_scheme;
  112     }
  113 
  114     // =-=-=-=-=-=-=-
  115     // init the hasher object
  116     irods::Hasher hasher;
  117     irods::error ret = irods::getHasher(
  118                            final_scheme,
  119                            hasher );
  120     if ( !ret.ok() ) {
  121         irods::log( PASS( ret ) );
  122         return ret.code();
  123 
  124     }
  125 
  126     // =-=-=-=-=-=-=-
  127     // open the local file
  128     std::ifstream in_file(
  129         _file_name,
  130         std::ios::in | std::ios::binary );
  131     if ( !in_file.is_open() ) {
  132         status = UNIX_FILE_OPEN_ERR - errno;
  133         rodsLogError(
  134             LOG_ERROR,
  135             status,
  136             "chksumLocFile - fopen failed for %s, status = %d",
  137             _file_name,
  138             status );
  139         return status;
  140     }
  141 
  142     std::string buffer_read;
  143     buffer_read.resize( HASH_BUF_SZ );
  144 
  145     while ( in_file.read( &buffer_read[0], HASH_BUF_SZ ) ) {
  146         hasher.update( buffer_read );
  147     }
  148 
  149     if ( in_file.eof() ) {
  150         if ( in_file.gcount() > 0 ) {
  151             buffer_read.resize( in_file.gcount() );
  152             hasher.update( buffer_read );
  153         }
  154     } else {
  155         status = UNIX_FILE_READ_ERR - errno;
  156         rodsLogError(
  157                      LOG_ERROR,
  158                      status,
  159                      "chksumLocFile - read() failed for %s, status = %d",
  160                      _file_name,
  161                      status );
  162         return status;
  163     }
  164 
  165     // =-=-=-=-=-=-=-
  166     // capture the digest
  167     std::string digest;
  168     hasher.digest( digest );
  169     strncpy(
  170         _checksum,
  171         digest.c_str(),
  172         digest.size() + 1 );
  173 
  174     return 0;
  175 
  176 } // chksumLocFile
  177 
  178 int verifyChksumLocFile(
  179     char *fileName,
  180     const char *myChksum,
  181     char *chksumStr ) {
  182     // =-=-=-=-=-=-=-
  183     // extract scheme from checksum string
  184     std::string scheme;
  185     irods::error ret = irods::get_hash_scheme_from_checksum( myChksum, scheme );
  186     if ( !ret.ok() ) {
  187         //irods::log( PASS( ret ) );
  188     }
  189 
  190     char chksumBuf[CHKSUM_LEN];
  191     if ( chksumStr == NULL ) {
  192         chksumStr = chksumBuf;
  193     }
  194 
  195     int status = chksumLocFile( fileName, chksumStr, scheme.c_str() );
  196     if ( status < 0 ) {
  197         return status;
  198     }
  199     if ( strcmp( myChksum, chksumStr ) != 0 ) {
  200         return USER_CHKSUM_MISMATCH;
  201     }
  202     return 0;
  203 }
  204 
  205 int
  206 hashToStr( unsigned char *digest, char *digestStr ) {
  207     int i;
  208     char *outPtr = digestStr;
  209 
  210     for ( i = 0; i < 16; i++ ) {
  211         sprintf( outPtr, "%02x", digest[i] );
  212         outPtr += 2;
  213     }
  214 
  215     return 0;
  216 }
  217 
  218 /* rcChksumLocFile - chksum a local file and put the result in the
  219  * condInput.
  220  * Input -
  221  *  char *fileName - the local file name
  222  *  char *chksumFlag - the chksum flag. valid flags are
  223  *         VERIFY_CHKSUM_KW and REG_CHKSUM_KW.
  224  *  keyValPair_t *condInput - the result is put into this struct
  225  */
  226 
  227 int
  228 rcChksumLocFile( char *fileName, char *chksumFlag, keyValPair_t *condInput, const char* _scheme ) {
  229     char chksumStr[NAME_LEN];
  230     int status;
  231 
  232     if ( condInput == NULL || chksumFlag == NULL || fileName == NULL ) {
  233         rodsLog( LOG_NOTICE,
  234                  "rcChksumLocFile: NULL input" );
  235         return USER__NULL_INPUT_ERR;
  236     }
  237 
  238     if ( strcmp( chksumFlag, VERIFY_CHKSUM_KW ) != 0 &&
  239             strcmp( chksumFlag, REG_CHKSUM_KW ) != 0 &&
  240             strcmp( chksumFlag, RSYNC_CHKSUM_KW ) != 0 ) {
  241         rodsLog( LOG_NOTICE,
  242                  "rcChksumLocFile: bad input chksumFlag %s", chksumFlag );
  243         return USER_BAD_KEYWORD_ERR;
  244     }
  245 
  246     status = chksumLocFile( fileName, chksumStr, _scheme );
  247     if ( status < 0 ) {
  248         return status;
  249     }
  250 
  251     addKeyVal( condInput, chksumFlag, chksumStr );
  252 
  253     return 0;
  254 }