QString Builder::CalcSha1(QString MCP_AbsolutePath) { //Création d'un objet Qt représentant un fichier pour le lire QFile FileToHash(MCP_AbsolutePath); //Creation d'un tableau de Bytes QByteArray MXL_Bytes; //Création d'un QCryptographicHash SHA1 QCryptographicHash CPU(QCryptographicHash::Sha1); //On ouvre le fichier if (FileToHash.open(QIODevice::ReadOnly)) { //on parcours le fichier jusqu'a la fin while (!FileToHash.atEnd()) { //On stoque dans le tableau de Byte 1024 Octects MXL_Bytes = FileToHash.read(1024); //on donne les Octects à manger au QCryptographicHash CPU.addData(MXL_Bytes); } FileToHash.close(); //On demande au QCryptographicHash de nous donner le hash (en Hexa) MXL_Bytes = CPU.result(); //On transforme l'hexa en String pour pas s'embeter avec QString MCL_HashOutput(MXL_Bytes.toHex()); return MCL_HashOutput; } else { emit NotifyErrorMsg("Impossible de claculer le Hash du fichier" + MCP_AbsolutePath); //Impossible d'ouvrir le fichier : on renvoie une String pourrave QString MCL_HashOutput("ERR"); return MCL_HashOutput; } }
QString TDownlad::CalcSha1(QString Path) { QFile FileToHash(Path); QByteArray ba; QString HashOutput; QCryptographicHash CPU(QCryptographicHash::Sha1); if (FileToHash.open(QIODevice::ReadOnly)) { while (!FileToHash.atEnd()) { ba = FileToHash.read(1024); CPU.addData(ba); } FileToHash.close(); ba = CPU.result(); HashOutput = ba.toHex(); return HashOutput; } else { HashOutput = "ERR"; return HashOutput; } }
BOOL FFileCompare( void) { long iLine1, iLine2; long iBlk; CBLK *pBlk; long cBlocks; /* convert all lines in both files to hash entries */ FileToHash(&fdd2); FileToHash(&fdd1); /* set each file phe to the appropriate hash entry */ SetAllBuffHE(&fdd1); SetAllBuffHE(&fdd2); /* find all common blocks of lines */ for (iLine1 = iLine2 = 0, iBlk = 1, pBlk = &rgCBlkFinal[iBlk]; iBlk < cCBlkFinal; iBlk++, pBlk++) { /* recurse on each block until we can't find any more common lines, * then loop to look at the next block */ for (cBlocks = 1; (cBlocks > 0) && ((pBlk->iLine1 - iLine1) > 1) && ((pBlk->iLine2 - iLine2) > 1);) { cBlocks = cCBLKFindCommonLines(iLine1, iLine2, pBlk->iLine1, pBlk->iLine2); pBlk = &rgCBlkFinal[iBlk]; /* in case rgCBlkFinal moved */ } iLine1 = pBlk->iLine1 + pBlk->cLine; iLine2 = pBlk->iLine2 + pBlk->cLine; } #ifdef DEBUGPRINT /* print out a list of the final group of common blocks */ printf("\n"); for (iBlk = 1, pBlk = &rgCBlkFinal[1]; iBlk < (cCBlkFinal - 1); iBlk++, pBlk++) { printf("final common block: iLine1 = %ld iLine2 = %ld cLine = %ld\r\n", pBlk->iLine1, pBlk->iLine2, pBlk->cLine); } printf("\n"); #endif /* if the beginning of each text buffer is not set to the beginning * of its file we must reinitialize that text buffer to the beginning * of the file for the check pass */ if (fdd1.iLineFirst != 0) { fdd1.iLineFirst = 0; fdd1.cLineBuff = 0; fdd1.oFileNextLine = 0; fdd1.fCtrlZ = fFalse; } if (fdd2.iLineFirst != 0) { fdd2.iLineFirst = 0; fdd2.cLineBuff = 0; fdd2.oFileNextLine = 0; fdd2.fCtrlZ = fFalse; } /* now walk through each block of common lines - output the * lines between each block since they are the ones that * changed and verify that what we thought were identical * lines are really identical (hey, even with a 32 bit hash * code, we sometimes get a duplicate hash for 2 different * lines) */ for (iBlk = 1, pBlk = &rgCBlkFinal[1]; iBlk < cCBlkFinal; iBlk++, pBlk++) { iLine1 = ((pBlk - 1)->iLine1 + (pBlk - 1)->cLine); iLine2 = ((pBlk - 1)->iLine2 + (pBlk - 1)->cLine); PrintDiffs(pBlk->iLine1 - iLine1, pBlk->iLine2 - iLine2, iLine1, iLine2); if (iBlk < (cCBlkFinal - 1)) CheckDiffBlk(pBlk); } if (!fFilesSame && fCkSum) { if (fCkSumErr) printf("\r\n#D CKSUM ERROR\r\n\r\n\r\n"); else printf("\r\n#D %11lu\r\n\r\n\r\n", ulCkSum); } return (fFilesSame); }