CheckSum crc32 value is changing for rebuild but it in linux qt C++ CRC32 value is not changing for rebuild. Please Solve this issue I need for same code after rebuild the check sum value should remain same
-
#include <QCoreApplication>
#include <QFile>
#include <QCryptographicHash>
#include <QDebug>
#include <sys/stat.h>
#include <time.h>
unsigned long reflect(unsigned long crc,int bitnum){
//Reflects the lower 'bitnum' bits of 'crc'
unsigned long i,j=1,crcout=0;
for(i=(unsigned long)1<<(bitnum-1);i;i>>=1){
if(crc & i) crcout|=j;
j<<=1;
}
return (crcout);
}
void generate_crc_table(unsigned long *crctab){
const int refin=1;
const int order=32;
const unsigned long polynom=0x4c11db7;
unsigned long crcmask;
unsigned long crchighbit;
//make crc lookup table used by table algorithms
int i,j;
unsigned long bit,crc;
crcmask=((((unsigned long)1<<(order-1))-1)<<1)|1;
crchighbit=(unsigned long)1<<(order-1);
for(i=0;i<256;++i){
crc=(unsigned long)i;
if(refin)
crc=reflect(crc,8);
crc<<=order-8;
for(j=0;j<8;j++){
bit=crc & crchighbit;
crc<<=1;
if(bit) crc^=polynom;
}
if(refin)
crc=reflect(crc,order);
crc&=crcmask;
crctab[i]=crc;
}
}unsigned int crc32(unsigned int crc,char *buf,unsigned int size){
unsigned long crctab[256];
const unsigned char *p;
generate_crc_table(crctab);
p=(unsigned char *)buf;
crc=crc ^ ~0U;
while(size--){
crc=crctab[(crc^*p++)& 0xFF]^(crc>>8);
}
qDebug()<<"Returned value crc32 "<<QString::number(crc ^ ~0U);
return (crc ^ ~0U);
}
QString fileinfo_crc(char *str){
struct stat *c,ct;
time_t t;
char timestr[40];
char *buf;
unsigned int err,crc32_val=0;
FILE *fp;
QString filename=str;
QString checksum1;
QStringList filenm=filename.split("\");
int n=filenm.count();
QString version1="1.0.4";
qDebug()<<version1;
fp=fopen(str,"rb");
c=&ct;
err=stat(str,c);
t=c->st_mtime;
strcpy(timestr,ctime(&t));
QString Date1=timestr;
qDebug()<<Date1;
buf=(char *)malloc(c->st_size);
qDebug()<<c->st_size;
fread(buf,1,c->st_size,fp);
qDebug()<<fp;
crc32_val=crc32(crc32_val,buf,(unsigned int)c->st_size);
qDebug()<<"crc32_val : "<<crc32_val;
checksum1="0x"+checksum1.setNum(crc32_val,16);
qDebug()<<"Check sum in fileinfo_crc"<<checksum1;
free(buf);
fclose(fp);
return checksum1;
}int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<argv[0];
QString checksum =fileinfo_crc(argv[0]);if(checksum.isEmpty()){ qDebug()<<"Unable to generate the check sum"; }else{ qDebug()<<"Check sum generated is "<<checksum; } return a.exec();
}
-
@HemanthKumar please wrap your code in code tags to improve readability. Also, please ask a clear question, right now I am not sure what your problem is.
-
@HemanthKumar This is the result of building your (ancient and fragile) C code on my Linux box and running it against its own executable twice with a complete rebuild in between. For comparison a CRC 32 generated by another tool is shown.
$ ./tt tt ./tt "1.0.4" "Wed Mar 27 18:13:20 2024\n" 391504 0x5a3fdccf6c20 Returned value crc32 "120820861" crc32_val : 120820861 Check sum in fileinfo_crc "0x733947d" Check sum generated is "0x733947d" ^C $ ./tt tt ./tt "1.0.4" "Wed Mar 27 18:18:32 2024\n" 391504 0x57d432491c20 Returned value crc32 "120820861" crc32_val : 120820861 Check sum in fileinfo_crc "0x733947d" Check sum generated is "0x733947d" ^C $ crc32 tt 0733947d
So:
- The CRC is the same with both builds of the executable: i.e. the executable does the same thing when rebuilt.
- The CRC is the same as the other utility (which is lucky given dozens of CRC32 variants out there),. The CRC algorithm is probably right.
Please explain the problem.
Edit: You do know that argv[0] is the name of the executable and not the file name passed as an argument... this executable always checksums itself.
On Windows, which I am guessing is your other environment, under MS Visual C++ argv[0] may not be the executable path depending on how the application is launched. How your program behaves in that circumstance is a different problem.
-
@ChrisW67 While running in the Windows qt C++ creator checksum value is changing for every rebuild even code remains same. When the same code run in Linux qt C++ creator checksum value is not changing for every rebuild for same code( Working as expected in linux ).
I need for every rebuild checksum should remains the same till any change in code happen in windows platform.
Note: I need to generate Checksum for my project which ensures Integrity of code to share with other clients. So I used CRC32 algorithm to generate checksum and the code I have provided above. For every rebuild of my project the check sum is changing even the code was not changed. Please solve this issue or give any alternative for integrity of code
-
Christian Ehrlicher Lifetime Qt Championreplied to HemanthKumar on last edited by Christian Ehrlicher
@HemanthKumar said in CheckSum crc32 value is changing for rebuild but it in linux qt C++ CRC32 value is not changing for rebuild. Please Solve this issue I need for same code after rebuild the check sum value should remain same:
Please solve this issue
There is nothing to solve here by Qt - it's the compiler which adds e.g. different timestamps to the executable. It's very hard to create a binary which is byte by byte the same after a recompilation. Mostly the compilers have special flags to achieve this. You have to search for this by yourself.
-
@HemanthKumar said in CheckSum crc32 value is changing for rebuild but it in linux qt C++ CRC32 value is not changing for rebuild. Please Solve this issue I need for same code after rebuild the check sum value should remain same:
... which ensures Integrity of code to share with other clients.
This is what code signing certificates are for. Sign the executable you built and tested and your client can verify that it came from you and that it has not changed since it was signed.