Answer the question
In order to leave comments, you need to log in
How to check if a file is corrupted in Python?
Hello users of Habr! I wrote in Python, using the cryptography library (Fernet), a simple file encoder. The essence is extremely simple - the file is encrypted with a key. But one problem was revealed: if you try to decrypt files twice in a row, they break irrevocably. A solution immediately popped into my head: check the file for damage
(this is how Windows perceived encrypted files) and only then decrypt it. But I could only find how to check the image for damage, and this is just part of what you need. Therefore, I ask you to help with this question and answer how to check any file for damage using Python.
Answer the question
In order to leave comments, you need to log in
My answer will be theoretical. We need to find out the hash of the whole file and remember it, and we also need a label that the file is encrypted, although the fact that the hash of the whole file is present can serve as such. For example, when encrypting, we can add a hash to the metadata, and when decrypting it, delete it, at the same time checking the integrity, comparing the hash of what happened with what it was.
Formally speaking, you can only look for damage in files that are protected from damage - most often, these are archives - they have a checksum.
Otherwise, you can only check that the file structure is correct. It is clear that for this you need to know the format of all files in the world, which is practically unrealizable.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question