0
0
Cybersecurityknowledge~5 mins

Hashing algorithms (SHA, MD5) in Cybersecurity - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Hashing algorithms (SHA, MD5)
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the input size grows, the number of steps grows in the same way.

Input Size (n)Approx. Operations
1010 updates
100100 updates
10001000 updates

Pattern observation: Doubling the input doubles the work needed.

Final Time Complexity

Time Complexity: O(n)

This means the time to hash grows directly with the size of the input data.

Common Mistake

[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.

Interview Connect

Understanding how hashing time grows helps you explain performance in security tasks clearly and confidently.

Self-Check

"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?"