<p>For the first question, about the <em>last</em> line, it surely depends on the hash function. For example, suppose each line is a single bit (0 or 1). If the hash function is the xor of the bits, then the answer is yes -- take 240 lines with total parity 0. But the answer is no if the hash function is defined by
<span class="math-container">$$f(x_1, x_2, \ldots, c_n) = \neg (x_n \oplus f(x_1, \ldots, x_{n-1})),$$</span>
with <span class="math-container">$f()=0$</span>, where <span class="math-container">$\oplus$</span> is xor. </p>

<p>For the second question (about <em>any line</em>), I think @Yonatan's point in his comment was that if the hash function has finitely many possible outputs (as e.g. MD5 does), then, for a file that already contains every possible hash output (one per line), no matter what the hash outputs it will be in the file already.</p>

CC BY-SA 4.0
2020-05-21T01:04:37.987Z
2020-05-21T01:04:37.987Z
Neal Young
<p>Suppose you have a file of 240 lines. Any lines, any content.
You then calculate the hash of that file, let's say MD5, and the result is something in the following structure:</p>

<pre><code>f8dbe310c1f61066d766071b07503ce8
</code></pre>

<p>Now, you take this hexadecimal digest, and add it to the file as line 241.</p>

<p>Rehashing the file (with additional hash line) will result with a new hash value obiously.</p>

<p>My questoin is whether it is even theoretically possible for a hash value to contain itself, meaning, can there be a file, with some content that its hashed value is identical to a string already presented in the file (e.g. line 241 in this example)</p>

CC BY-SA 4.0
2020-05-20T08:44:25.143Z
Can the program be $2^{128}$ lines long?
Yonatan N
CC BY-SA 4.0
2020-05-20T11:08:22.413Z
assuming it can...
Tzury Bar Yochay
CC BY-SA 4.0
2020-05-20T18:53:07.050Z
For the question about the _last_ line, it surely depends on the hash function.  For example, suppose each line is a single bit (0 or 1).  If the hash function is the xor of the bits, then the answer is yes -- take 240 lines with total parity 0.  But the answer is no if the hash function is defined by $$f(x_1, x_2, \ldots, x_n) = \neg (x_n \oplus f(x_1, x_2, \ldots, x_{n-1}))$$ with $f() = 0$.  (Here $\oplus$ is xor.)  BTW I think Yonatan's point was that if the file already contains every possible hash output (one per line), then no matter what the hash outputs it will be in the file already.
Neal Young
CC BY-SA 4.0
2020-05-20T20:28:50.777Z
Than kyou @NealYoung! may you change this comment to an answer so I can mark it properly ?
Tzury Bar Yochay
<hash-function>
Can a result of (any) hash algorithm contain the hash result itself?
Tzury Bar Yochay
# Can a result of (any) hash algorithm contain the hash result itself?

(Asked by: username_0 on 2020-05-20)

Suppose you have a file of 240 lines. Any lines, any content.
You then calculate the hash of that file, let's say MD5, and the result is something in the following structure:
f8dbe310c1f61066d766071b07503ce8

Now, you take this hexadecimal digest, and add it to the file as line 241.
Rehashing the file (with additional hash line) will result with a new hash value obiously.
My questoin is whether it is even theoretically possible for a hash value to contain itself, meaning, can there be a file, with some content that its hashed value is identical to a string already presented in the file (e.g. line 241 in this example)

**Tags:** hash-function

> **Comments:**
> **username_1**: Can the program be $2^{128}$ lines long?  
> **username_0**: assuming it can...  
> **username_2**: For the question about the _last_ line, it surely depends on the hash function.  For example, suppose each line is a single bit (0 or 1).  If the hash function is the xor of the bits, then the answer is yes -- take 240 lines with total parity 0.  But the answer is no if the hash function is defined by $$f(x_1, x_2, \ldots, x_n) = \neg (x_n \oplus f(x_1, x_2, \ldots, x_{n-1}))$$ with $f() = 0$.  (Here $\oplus$ is xor.)  BTW I think Yonatan's point was that if the file already contains every possible hash output (one per line), then no matter what the hash outputs it will be in the file already.  
> **username_0**: Than kyou @NealYoung! may you change this comment to an answer so I can mark it properly ?  

---

## 1 answers

### Answer by username_2 on 2020-05-21. Score: 4 | Accepted answer

For the first question, about the *last* line, it surely depends on the hash function. For example, suppose each line is a single bit (0 or 1). If the hash function is the xor of the bits, then the answer is yes -- take 240 lines with total parity 0. But the answer is no if the hash function is defined by
$$f(x\_1, x\_2, \ldots, c\_n) = \neg (x\_n \oplus f(x\_1, \ldots, x\_{n-1})),$$
with $f()=0$, where $\oplus$ is xor.

For the second question (about *any line*), I think @Yonatan's point in his comment was that if the hash function has finitely many possible outputs (as e.g. MD5 does), then, for a file that already contains every possible hash output (one per line), no matter what the hash outputs it will be in the file already.


---

