Hashing algorithms (SHA, MD5) in Cybersecurity - Time & Space Complexity
When we use hashing algorithms like SHA or MD5, we want to know how the time to create a hash changes as the input data grows.
We ask: How much longer does it take to hash bigger files or messages?
Analyze the time complexity of this hashing process.
function hashData(data) {
let hash = initializeHash();
for (let i = 0; i < data.length; i++) {
hash = updateHash(hash, data[i]);
}
return finalizeHash(hash);
}
This code takes each piece of data and updates the hash step by step until done.
Look at what repeats as the input grows.
- Primary operation: Looping through each data element to update the hash.
- How many times: Exactly once for every item in the input data.
As the input size grows, the number of steps grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 100 | 100 updates |
| 1000 | 1000 updates |
Pattern observation: Doubling the input doubles the work needed.
Time Complexity: O(n)
This means the time to hash grows directly with the size of the input data.
[X] Wrong: "Hashing always takes the same time no matter the input size."
[OK] Correct: Actually, hashing processes each piece of data, so bigger inputs take more time.
Understanding how hashing time grows helps you explain performance in security tasks clearly and confidently.
"What if the hashing algorithm processed data in fixed-size blocks instead of one element at a time? How would that affect the time complexity?"