there. I'm preparing a Laravel test, and there's a question that I think is not correct. When you should use a hash? The available answers are:
- When you want to compress the contents of a file.
- When you want to securely store credit card information so you can use it later.
- When you want to secure sen a password over email.
- When you want to identify the contents of a file without storing the entire file
Since hashing is for encrypting passwords (not to send'em over email) none of this answers seem to be correct. What do you think?
CodePudding user response:
Option 4. Identifying contents of a file.
Hash is a function which is supposed to return a constant length output for every input. The other property of hash functions is that for any input a
it always returns the same value b
. It means that if you provide file a
and store its hash b
then whenever you supply file a
again you're going to get hash b
. The last property is that for different inputs c
, d
and hash function f
, f(c)
should be different from f(d)
(or the chances of outputs being equal should be near 0)
In real case scenario, you can often find hashes when downloading software and want to verify if the file you've downloaded is correct. The developer puts a hash of the executable on their site. You are downloading the file and calculate checksum (hash) just to compare it with the one from the website. If it matches, then you know it's the same (as long as the hash algorithm is not known to have collisions...).
It is quite good approach to comparing file contents, bc hashes are taking much less space than actual files.