testfile - then xxd...">

Same contents, different MD5 - file and line

  • I have a testfile and a string teststring .

  • In the shell, I wrote:
    echo "a" > testfile

  • then xxd testfile
    so i can see the hexadecimal values โ€‹โ€‹of my filecontent
    Output:

     0000000: 610a a. 
  • see my code:

     int file; struct stat s; unsigned long size; char* buffer; char md5[MD5_DIGEST_LENGTH] file = open("testfile", O_RDONLY); if (file < 0) return false; if (fstat(file, &s) < 0) { close(file); return false; } size = s.st_size; //GET FILE SIZE printf("filesize: %lu\n", size); //PRINT FILESIZE FOR DEBUGGING buffer = (char*)mmap(0, size, PROT_READ, MAP_SHARED, file, 0); //MAP FILE CONTENT TO BUFFER MD5((unsigned char*)buffer, size, md5); //GENERATE MD5 munmap(buffer, size); //UNMAP BUFFER close(file); for (int i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", md5[i]); printf("\n"); unsigned char* teststring = "\x61\x0a"; //SAME STRING AS IN THE FILE MD5((unsigned char*)teststring, 2, md5); for (int i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", md5[i]); printf("\n"); 
  • he prints:

     filesize: 2 60b725f10c9c85c70d97880dfe8191b3 e29311f6f1bf1af907f9ef9f44b8328b 

    two completely different md5 hashes.
    I tried to write buffer to a file
    and writing teststring to a file , they are the same !
    why? is not buffer the same as teststring ?

+6
source share
1 answer

The correct hash is your first hash, 60b725f10c9c85c70d97880dfe8191b3 .

 $ echo "a" | md5 60b725f10c9c85c70d97880dfe8191b3 

Your second hash is the hash "\ x64 \ x0a" or the character "d", followed by a new line:

 $ echo "d" | md5 e29311f6f1bf1af907f9ef9f44b8328b 

Are you sure that the code you posted is what you compile / execute? Did you forget to recompile? Are you executing the old binary?

+2
source

All Articles