Difficulty: Medium
Correct Answer: crc32() produces a 32 bit checksum mainly for error detection, md5() produces a 128 bit hash, and sha1() produces a 160 bit hash that was historically considered stronger; they differ in output length, speed, and suitability for security tasks.
Explanation:
Introduction / Context:
This question compares three PHP functions that create condensed representations of data: md5(), crc32(), and sha1(). These are often described together in interview questions to see whether you understand their different output sizes and how they are used in practice. Modern security standards discourage using md5() and sha1() for passwords, but they still appear in legacy code and non security contexts.
Given Data / Assumptions:
Concept / Approach:
crc32() calculates a 32 bit cyclic redundancy check value that is useful for detecting accidental changes in data, such as transmission errors. It is not a cryptographic hash and is not suitable for resisting deliberate attacks. md5() computes a 128 bit hash value, typically represented as a 32 character hexadecimal string. sha1() computes a 160 bit hash value, usually shown as a 40 character hexadecimal string. Both md5() and sha1() were designed as cryptographic hash functions, but weaknesses have been found, making them unsuitable for high security uses such as password storage without additional safeguards.
Step-by-Step Solution:
Step 1: Recognise that crc32() is a checksum function with a small 32 bit output, mainly used for error checking.Step 2: Recall that md5() outputs 128 bit hashes and is frequently represented by 32 hexadecimal characters.Step 3: Recall that sha1() outputs 160 bit hashes, which are represented by 40 hexadecimal characters.Step 4: Understand that longer hashes provide more possible values and therefore more resistance to random collisions, although implementation details also matter.Step 5: Compare the answer choices and select the one that correctly summarises these differences in size and typical use, which is option A.
Verification / Alternative check:
You can verify the lengths by running echo strlen(md5("test")); which returns 32, and echo strlen(sha1("test")); which returns 40. If you cast crc32("test") to an integer, you can see that the result fits within 32 bits. Documentation also confirms that crc32() is primarily a checksum, while md5() and sha1() are general purpose hash functions with larger outputs.
Why Other Options Are Wrong:
Option B claims that all three functions produce identical outputs for the same input, which is incorrect; each algorithm has its own mapping. Option C reverses the roles of the functions and assigns them tasks they do not perform. Option D mislabels the functions as formatting, date, and concatenation helpers, which is completely unrelated to their real purpose.
Common Pitfalls:
A common mistake is to use md5() or sha1() directly for password storage without salts or stronger key stretching, which is insecure by modern standards. Another pitfall is to treat crc32() as if it provides security when it was never designed to do so. Developers should choose algorithms based on current security guidance and use these older functions only for non sensitive tasks like simple checksums or indexing.
Final Answer:
crc32() produces a 32 bit checksum mainly for error detection, md5() produces a 128 bit hash, and sha1() produces a 160 bit hash that was historically considered stronger; they differ in output length, speed, and suitability for security tasks.
Discussion & Comments