1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| bool bHashDone = false;
std::ofstream fileHash;
std::ifstream file;
if( NULL != m_NameFile.c_str() )
{
try
{
SystemConfiguration::COutilsCommun::OpenOStream(fileHash,m_NameFileHash.c_str());
SystemConfiguration::COutilsCommun::OpenIStream(file, m_NameFile.c_str());
std::ofstream fileHash( m_NameFileHash.c_str(), std::ofstream::out|std::ofstream::trunc );
std::ifstream file( m_NameFile.c_str() );
// if std::ifstream file is huge ( I have one error then I must read the
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;
}
unsigned char digest[SHA512_DIGEST_LENGTH];
std::string mdString(SHA512_DIGEST_LENGTH*2,' ');
std::stringstream buffer; //// variable with the content of the file
if( (file)&& (fileHash) )
{
buffer << file.rdbuf();
SHA512_CTX ctx;
SHA512_Init( &ctx );
SHA512_Update( &ctx, buffer.str().c_str(), strlen(buffer.str().c_str()) );
SHA512_Final(digest, &ctx);
char* bufInStr = const_cast<char*>(mdString.data());
for ( int i = 0 ; i < SHA512_DIGEST_LENGTH ; ++i ) {
// FYI : don't use sprintf_s() because \0 can't be inside std::string
const char tabCaractHexa[] = "0123456789abcdef";
int valueToConvert = digest[i];
int first = (valueToConvert >> 4) & 0xF;
int second = valueToConvert & 0xF;
bufInStr[i*2] = tabCaractHexa[first];
bufInStr[i*2+1] = tabCaractHexa[second];
}
bHashDone = true;
fileHash << mdString;
std::cout <<"SHA512 digest of " << m_NameFile.c_str() << " is "<< mdString << std::endl;
} |
Partager