0

I want to do the hash of a 7z file. I can open the file in binary and get the hash of a archive .7z if inside this .7z file.

If I have 2 small .txt files inside the archive .7z then the hash is not good. If I want to do the hash of one archive .7z of a big .txt file like 1.8GB, it is the same I have a hash that is not good .

With a big file I do only one loop here, that is no good :

while(!file.eof())      
{
    char buff[bufferSize];
    memset(buff,0x00,sizeof(buff));
    file.read(buff,bufferSize);
    size_t nbRead = file.gcount(); 
    SHA512_Update( &ctx, &buff[0], nbRead );
}

Can you help me about how to read this .7z file ?

Thank you for your help


Here is the source code.

#include <atlstr.h>
#include <LibOpenSSL.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <iostream>  
using namespace std;

my function :

bool HashFile::generateHashInFile() 
{
    bool bHashDone = false;
    std::ofstream fileHash;
    std::ifstream file;


 // I do this to try to hash 6 files  
      for(int i=0;i<6;i++)
{
          if(i==0){
              m_NameFileHash="D:\\TODELETE\\0.sha512";
              m_NameFile="D:\\TODELETE\\0.7z";
          }
          if(i==1){
              m_NameFileHash="D:\\TODELETE\\1.sha512";
              m_NameFile="D:\\TODELETE\\1.7z";
          }
          if(i==2){
              m_NameFileHash="D:\\TODELETE\\2.sha512";
              m_NameFile="D:\\TODELETE\\2.7z";
          }
          if(i==3){
              m_NameFileHash="D:\\TODELETE\\3.sha512";
              m_NameFile="D:\\TODELETE\\3.7z";
          }
          if(i==4){
              m_NameFileHash="D:\\TODELETE\\4.sha512";
              m_NameFile="D:\\TODELETE\\4.7z";
          }
          if(i==5){
              m_NameFileHash="D:\\TODELETE\\5.sha512";
              m_NameFile="D:\\TODELETE\\5.7z";
          }


          fileHash.clear();
          file.clear();
          SystemConfiguration::COutilsCommun::OpenOStream(fileHash,m_NameFileHash.c_str());
          SystemConfiguration::COutilsCommun::OpenIStream(file, m_NameFile.c_str());
//this is useful to open a stream  SystemConfiguration::COutilsCommun::OpenOStream(fileHash,m_NameFileHash.c_str()); 

         // if std::ifstream file is huge ( I have one error )
         if(!file)
         {
             std::cout << "impossible d'ourvrir le fichier : " << m_NameFile.c_str() <<std::endl;
             std::cout << "taille du fichier : " << file.tellg() <<std::endl;
         }      
         if(!fileHash)
         {
             std::cout << "impossible de creer le fichier : " << m_NameFileHash.c_str() <<std::endl;
         }   


         if( (file)&& (fileHash) )
         {          
             unsigned char digest[SHA512_DIGEST_LENGTH];
             std::string mdString(SHA512_DIGEST_LENGTH*2,' ');
             std::stringstream buffer; //// variable with the content of the file 

           // buffer << file.rdbuf();


             const size_t bufferSize = 1024;
             //vector<char> bufferX(bufferSize); 
             //char buff[bufferSize];
             char * finalBuff = NULL;

             SHA512_CTX ctx;
             SHA512_Init( &ctx );

             if(!file.is_open())
             {
                 cout << "Unable to open file" << endl;
                 bHashDone = false;
             }

             file.seekg(0,std::ios::beg);
             while(!file.eof())
             {
                 char buff[bufferSize];
                 memset(buff,0x00,sizeof(buff));
                 file.read(buff,bufferSize);
                 size_t nbRead = file.gcount(); 
                 SHA512_Update( &ctx, &buff[0], nbRead );
             }


             SHA512_Final(digest, &ctx);
             bHashDone = true;


            vector<char> szBuffer(SHA512_DIGEST_LENGTH*2 + 1);
            for ( int i = 0 ; i < SHA512_DIGEST_LENGTH ; ++i ) {
                const char tabCaractHexa[] = "0123456789abcdef";
                int valueToConvert = digest[i];
                int first = (valueToConvert >> 4) & 0xF; // part 'hight' of the byte
                int second  = valueToConvert & 0xF;      // lower part  -> base unit 16
                szBuffer[i*2]   = tabCaractHexa[first];  // conversion 0 to 15 in '0' to 'F'
                szBuffer[i*2+1] = tabCaractHexa[second]; // conversion 0 to 15 in '0' to 'F'
            }
            szBuffer[SHA512_DIGEST_LENGTH*2] = '\0';
            std::string mdString2(&szBuffer[0]);
            cout << "SHA512 digest of  " << m_NameFile << " is " << mdString2 << endl;
            fileHash << mdString2;

            //clean the content of the variables     
            buffer.clear();
            mdString.clear();   
            mdString2.clear();
            m_NameFile.clear();
            m_NameFileHash.clear();
         } //end if( (file)&& (fileHash) )

         if( file.is_open() )
         {
             SystemConfiguration::COutilsCommun::CloseIStream(file,m_NameFile.c_str());
         }
         if( fileHash.is_open() )
         {
             SystemConfiguration::COutilsCommun::CloseOStream(fileHash,m_NameFileHash.c_str());
         }


         fileHash.close();
         file.close();

}//le for pour test
         return bHashDone;
      }
      catch ( std::exception e)
      {
         std::cout << "impossible d'ourvrir le fichier : " << m_NameFile.c_str() <<std::endl;

         if( file.is_open() )
         {
             SystemConfiguration::COutilsCommun::CloseIStream(file,m_NameFile.c_str());
         }
         if( fileHash.is_open() )
         {
             SystemConfiguration::COutilsCommun::CloseOStream(fileHash,m_NameFileHash.c_str());
         }
}
         return bHashDone;    
   }
   else
   {
      std::cout <<  "\n\n Merci de specifier un nom de fichier à hasher et un nom de fichier qui contient le hash \n\n"  <<std::endl;
      return bHashDone;
   }    
}
jww
  • 83,594
  • 69
  • 338
  • 732
FI0RlAN-X
  • 73
  • 2
  • 9
  • 2
    possibly related: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – kmdreko Jun 02 '17 at 18:04
  • `while(!file.eof())` is not correct. See [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/q/5605125/608639), [Reading from text file until EOF repeats last line](https://stackoverflow.com/q/21647/608639), [How does ifstream's eof() work?](https://stackoverflow.com/q/4533063/608639) and all the related posts. – jww Jun 02 '17 at 19:44
  • I have try with !(file>>std::ws).eof() but this doesn't work. What can i do to replace this "while(!file.eof()) and"? – FI0RlAN-X Jun 05 '17 at 13:27

0 Answers0