Is 512 bits a more secure hashing than 256 bits? I know that 512 bit hashing is more secure, but I don't really know why. I hope someone can help me to better understand it in more detail.
When there is no known weakness in the hash function we talk about their generic1 resistances

Pre-image resistant: given a hash value $h$ find a message $m$ such that $h=Hash(m)$. E.g., consider storing the hashes of passwords on the server. An attacker will try to find a valid password to your account.

Second Pre-image resistant: given a message $m_1$ is should be computationally infeasible to find another message $m_2$ such that $m_1 \neq m_2$ and $Hash(m_1)=Hash(m_2)$, e.g., producing a forgery of a given message.

Collision resistance : it is hard to find two distinct inputs, $a$ and $b$, that hash to the same output $a$ and $b$, i.e., $H(a)= H(b)$ and $a \neq b.$


A hash function with output size $n$ is expected to have $\mathcal{O}(2^n)$ pre-image resistance and secondary image resistance and $\mathcal{O}(\sqrt{2^n)} = \mathcal{O}(2^{n/2})$ collision resisance. The collision resistance is less due to the birthday paradox (attack).
If we plug 256 and 512 into those equations you will see that

256 has $\mathcal{O}(2^{256})$ pre and secondary and $\mathcal{O}(2^{128})$ collision resistance, whereas
512 has $\mathcal{O}(2^{512})$ pre and secondary and $\mathcal{O}(2^{256})$ collision resistances.

As a result, as long as there is no specific weakness in the (unspecified) hash function the larger the output the better the resistance they will have.
When the hash function is specified then one can talk about their resistance in all directions. For example;

Standard Merkle–Damgård constructions (MD) (MD5, SHA-1, SHA-2) are vulnerable to length extension attacks, however, sponge constructions (SHA3) and HAIFA based (e.g. BLAKE(2)) are not. Note that after SHA3, there are variants of SHA-2 that have resistance against length extension attack like SHA512-256,

Herding attack for MD constructions is introduced by Kelsey and Kohno in 2005 and applied to MD5 Finding Preimages in Full MD5 Faster Than Exhaustive Search with Chosen Target Forced Prefix Pre-image resistance

Chosen-prefix collision on SHA-1 is a Shambles - First Chosen-Prefix Collision on SHA-1 and Application to the PGP Web of Trust, 2020. List of attacks on SHA-1 can be found 1,2



The relations of the generic resistances are, in simple terms,

Collision resistance implies 2nd-preimage resistance of hash functions, see a detailed work on our site

2nd-preimage resistance implies preimage resistance, see the proof on this site.


The formal definitions of the resistance and modern cryptographic deal of the above facts and more provided in the novel article; Cryptographic Hash-Function Basics: Definitions, Implications, and Separations for Preimage Resistance, Second-Preimage Resistance, and Collision Resistance by P. Rogaway and T. Shrimpton on 2009.

1 The term generic is used when a resistance ( or the attacks) applies all of the hash functions. This term similarly used in other cryptographic contexts, too.