A
A
Andrey Vershinin2014-04-13 16:24:45
Hashing
Andrey Vershinin, 2014-04-13 16:24:45

A text file containing its own hash - is this possible?

Reading one well-known resource dedicated to 'extraordinary' code, and seeing one instance of it, the question arose - is it possible for a string to contain a substring that is its hash?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sheknitrtch, 2014-04-13
@sheknitrtch

In a nutshell, this can be done with the following "stupid" algorithm:
# Come up with a hash function value. For example: final_hash = '4817504d069b4c5082161b02a22116ad75f822b1'
# Start iterating by adding arbitrary strings to the source string with the hash:
txt = final_hash + 'a'
txt = final_hash + 'b'
txt = final_hash + 'c'
...
txt = final_hash + 'z '
txt = final_hash + 'aa'
txt = final_hash + 'ab'
...
# Calculate hash value for each option and compare with final_hash. As soon as calc_hash(txt) == final_hash, it means that you have found the desired string, which contains its own hash.
The problem is,
Perhaps there is some clever iteration using rainbow tables .
Much depends on the type of hash function.

P
Pavel Volintsev, 2014-04-14
@copist

First: if you saw the algorithm - try and make sure.
Secondly, if a file contains a hash of itself including the hash, then someone has chosen a not very successful hashing algorithm that has many collisions and spent a lot of time selecting the hash string. Accordingly, it is possible to select a new hash for the modified file, and it will take as much time as it took the sender to calculate the hash of the original file. Rainbow tables probably won't help, because in this case the hash is not a function of the salt, but of the hash itself.
Third: a file may contain a hash not of itself, but of some part of its file.
For example, the first N bytes might be the hash of the entire file, excluding those N bytes.
To some extent, the checksums of files in ZIP are hashes from files, but all this is stored in one zip.
All this can be useful if you need to check the integrity of the document (an identical recomputed hash value for the same file means no transfer loss) or verify the authenticity of the data source (the hash was calculated by a known sender).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question