File containing its own checksum

后端 未结 12 1662
误落风尘
误落风尘 2020-12-23 13:16

Is it possible to create a file that will contain its own checksum (MD5, SHA1, whatever)? And to upset jokers I mean checksum in plain, not function calculating it.

相关标签:
12条回答
  • 2020-12-23 13:58

    Check this:

    echo -e '#!/bin/bash\necho My cksum is 918329835' > magic
    
    0 讨论(0)
  • 2020-12-23 13:59

    Sure, you could concatenate the digest of the file itself to the end of the file. To check it, you would calculate the digest of all but the last part, then compare it to the value in the last part. Of course, without some form of encryption, anyone can recalculate the digest and replace it.

    edit

    I should add that this is not so unusual. One technique is to concatenate a CRC-32 so that the CRC-32 of the whole file (including that digest) is zero. This won't work with digests based on cryptographic hashes, though.

    0 讨论(0)
  • 2020-12-23 14:02

    If the question is asking whether a file can contain its own checksum (in addition to other content), the answer is trivially yes for fixed-size checksums, because a file could contain all possible checksum values.

    If the question is whether a file could consist of its own checksum (and nothing else), it's trivial to construct a checksum algorithm that would make such a file impossible: for an n-byte checksum, take the binary representation of the first n bytes of the file and add 1. Since it's also trivial to construct a checksum that always encodes itself (i.e. do the above without adding 1), clearly there are some checksums that can encode themselves, and some that cannot. It would probably be quite difficult to tell which of these a standard checksum is.

    0 讨论(0)
  • 2020-12-23 14:04

    Certainly, it is possible. But one of the uses of checksums is to detect tampering of a file - how would you know if a file has been modified, if the modifier can also replace the checksum?

    0 讨论(0)
  • 2020-12-23 14:09

    I created a piece of code in C, then ran bruteforce for less than 2 minutes and got this wonder:

    The CRC32 of this string is 4A1C449B
    

    Note the must be no characters (end of line, etc) after the sentence.

    You can check it here: http://www.crc-online.com.ar/index.php?d=The+CRC32+of+this+string+is+4A1C449B&en=Calcular+CRC32

    This one is also fun:

    I killed 56e9dee4 cows and all I got was...
    

    Source code (sorry it's a little messy) here: http://www.latinsud.com/pub/crc32/

    0 讨论(0)
  • 2020-12-23 14:11

    I don't know if I understand your question correctly, but you could make the first 16 bytes of the file the checksum of the rest of the file.

    So before writing a file, you calculate the hash, write the hash value first and then write the file contents.

    0 讨论(0)
提交回复
热议问题