Can a large file be hashed down to 32 bytes, and then reconstructed from the hash? We can hash a file or data using multihash or SHA-256, but can we retrieve the original data or file from the hash?

Are there any methods to retrieve the original file or data from a hash of it without using IPFS?

Or is there any encryption method which encrypts a 5 MB file and outputs a hash-like content of 32 bytes so that we can retrieve the original file from that 32 byte content?
No, there is no way to compress (or hash or encrypt or whatever) a 5 MB file into a 32 byte hash and then reconstruct the original file just from the hash alone.

This is simply because there are many more possible 5 MB files than there are 32 byte hashes.  This means that, whatever hashing or compression or other algorithm you use, it must map many different 5 MB files to the same 32 byte hash.  And that means that, given only the 32 byte hash, there is no way you can possibly tell which of those different 5 MB files it was created from.

In fact, the same thing happens already if you hash 33 byte files into 32 byte hashes, and then try to reconstruct the original files from the hashes.  Since there are 256 times as many 33 byte files as there are 32 byte hashes, that already means that there must be several different files that have the same hash.  With 5 MB files, it's many, many, many times worse yet.



So how can something like IPFS work, then?

Basically, it relies on the fact that even the number of possible 32 byte hashes is really huge* &mdash; much, much larger than the total number of actual files (of any length) that humans have ever created, or are ever likely to create.  So, while we know that there must be many possible files that have the same 32 byte hash, the chance of actually finding two different files that just happen to have the same hash by chance is still so incredibly small that we can basically assume it will never happen.

(Also, cryptographic hash functions like SHA-256 are designed so that, hopefully, there are no practical ways to deliberately find files with the same hash more efficiently than by just hashing lots of files and hoping against all odds for a random collision.)

This means that if we have some kind of a (possibly distributed) database containing a bunch of files and their SHA-256 hashes, then we can be pretty sure that it will never actually contain two files with the same hash, even if that's theoretically possible.

Thus, as long as we have access to such a database, we can use the hash of any file in the database to look it up, and be almost 100% certain that we will only get one matching file back, not two or more.  Technically, the probability of getting multiple matches is not quite exactly zero, but it's so incredibly small that it can be safely neglected in practice.



*) In fact, it's 28&times;32 = 115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,665,640,564,039,457,584,007,913,129,639,936.