Bird
Raised Fist0
Cybersecurityknowledge~10 mins

Hashing algorithms (SHA, MD5) in Cybersecurity - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Hashing algorithms (SHA, MD5)
Input Data
↓
Apply Hash Function
↓
Process Data in Blocks
↓
Perform Mathematical Operations
↓
Generate Fixed-Size Hash Output
↓
Output Hash Value
Data is taken as input, processed through a hash function in blocks, mathematical operations create a fixed-size output called a hash.
Execution Sample
Cybersecurity
Input: "hello"
Apply MD5 hash function
Output: 5d41402abc4b2a76b9719d911017c592
This shows how the string "hello" is converted into a fixed-size MD5 hash.
Analysis Table
StepActionInput/StateOutput/Result
1Receive input data"hello""hello"
2Convert input to bytes"hello"[104, 101, 108, 108, 111]
3Process data in 512-bit blocks[104, 101, 108, 108, 111]Single block padded
4Perform MD5 compression functionPadded blockIntermediate hash state
5Repeat for all blocks (only one here)Intermediate hash stateFinal hash value
6Output hashFinal hash value"5d41402abc4b2a76b9719d911017c592"
💡 All input processed; final fixed-size hash output produced.
State Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
input_data"hello""hello""hello""hello""hello""hello"
byte_arrayN/A[104,101,108,108,111][104,101,108,108,111, padding][intermediate state][intermediate state]N/A
hash_stateN/AN/AInitial constantsUpdated stateFinal state"5d41402abc4b2a76b9719d911017c592"
Key Insights - 3 Insights
Why does the output hash always have the same length regardless of input size?
Because hashing algorithms like MD5 and SHA process data in blocks and produce a fixed-size output, as shown in execution_table steps 3 to 6.
Is it possible to get the original input from the hash output?
No, hashing is one-way; the execution_table shows only output generation, no reverse step exists.
Why do we add padding to the input data before hashing?
Padding ensures the input fits into fixed-size blocks required by the hash function, as seen in step 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after step 6?
A"5d41402abc4b2a76b9719d911017c592"
B"hello"
C[104, 101, 108, 108, 111]
DIntermediate hash state
💡 Hint
Check the 'Output/Result' column for step 6 in the execution_table.
At which step is the input data converted into bytes?
AStep 1
BStep 2
CStep 4
DStep 6
💡 Hint
Look at the 'Action' column in execution_table for the step mentioning conversion to bytes.
If the input was longer and required multiple blocks, which step would repeat?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Refer to execution_table step 5 mentioning repetition for all blocks.
Concept Snapshot
Hashing algorithms take any input data and produce a fixed-size string called a hash.
MD5 outputs 128-bit hashes; SHA variants output longer hashes (e.g., SHA-256).
They process data in blocks with padding to fit block size.
Hashes are one-way: you cannot get original data from the hash.
Used for data integrity, password storage, and digital signatures.
Full Transcript
Hashing algorithms like MD5 and SHA take input data and convert it into a fixed-size hash value. The process involves converting the input into bytes, padding it to fit fixed-size blocks, and then applying mathematical operations to produce a unique hash. This hash is always the same length regardless of input size. The process is one-way, meaning you cannot reverse the hash to get the original input. This makes hashing useful for verifying data integrity and securely storing passwords.

Practice

(1/5)
1. What is the primary purpose of hashing algorithms like MD5 and SHA in cybersecurity?
easy
A. To compress files to save storage space
B. To create a fixed-size code representing data for integrity verification
C. To encrypt data so only authorized users can read it
D. To generate random passwords automatically

Solution

  1. Step 1: Understand hashing function role

    Hashing algorithms convert any input data into a fixed-size string of characters, called a hash, which uniquely represents the original data.
  2. Step 2: Identify the main use in cybersecurity

    This fixed-size hash is mainly used to verify data integrity, ensuring data has not been altered.
  3. Final Answer:

    To create a fixed-size code representing data for integrity verification -> Option B
  4. Quick Check:

    Hashing = Data integrity check [OK]
Hint: Hashing creates unique codes to check data changes [OK]
Common Mistakes:
  • Confusing hashing with encryption
  • Thinking hashing compresses data
  • Believing hashing generates passwords
2. Which of the following is the correct way to represent the MD5 hash of the string "hello"?
easy
A. A variable-length string depending on input size
B. A plain text copy of the original string
C. A 256-bit binary string
D. A 128-bit fixed-length hexadecimal string

Solution

  1. Step 1: Recall MD5 hash size

    MD5 produces a fixed-size hash of 128 bits, usually shown as a 32-character hexadecimal string.
  2. Step 2: Compare options with MD5 output format

    The 128-bit fixed-length hexadecimal string correctly describes MD5 output as a fixed-length hexadecimal string.
  3. Final Answer:

    A 128-bit fixed-length hexadecimal string -> Option D
  4. Quick Check:

    MD5 hash length = 128 bits [OK]
Hint: MD5 always outputs 128-bit hex strings [OK]
Common Mistakes:
  • Thinking MD5 output length varies
  • Confusing MD5 with SHA-256 length
  • Assuming output is plain text
3. Consider this Python code snippet using hashlib:
import hashlib
hash_object = hashlib.sha256(b'password123')
hex_dig = hash_object.hexdigest()
print(hex_dig)

What will this code output?
medium
A. A 64-character hexadecimal string representing the SHA-256 hash of 'password123'
B. The original string 'password123'
C. An error because of incorrect input type
D. A 32-character hexadecimal string representing the MD5 hash

Solution

  1. Step 1: Understand hashlib usage

    The code uses hashlib.sha256 to hash the byte string b'password123'. The hexdigest() method returns the hash as a hexadecimal string.
  2. Step 2: Identify output length and content

    SHA-256 produces a 256-bit hash, which is 64 hex characters long. The output will be this 64-character hex string representing the hash.
  3. Final Answer:

    A 64-character hexadecimal string representing the SHA-256 hash of 'password123' -> Option A
  4. Quick Check:

    SHA-256 output length = 64 hex chars [OK]
Hint: SHA-256 hashes output 64 hex characters [OK]
Common Mistakes:
  • Expecting original string as output
  • Confusing input type causing error
  • Mixing MD5 and SHA-256 output lengths
4. A developer wrote this code to hash a password using MD5:
import hashlib
password = 'mypassword'
hash = hashlib.md5(password)
print(hash.hexdigest())

What is the error in this code?
medium
A. The print statement syntax is incorrect
B. MD5 does not have a hexdigest() method
C. The input to md5() must be bytes, not a string
D. The hashlib module does not support MD5

Solution

  1. Step 1: Check input type for hashlib.md5()

    hashlib.md5() requires a bytes-like object, but the code passes a string directly.
  2. Step 2: Identify correct fix

    The string should be encoded to bytes using password.encode() before passing to md5().
  3. Final Answer:

    The input to md5() must be bytes, not a string -> Option C
  4. Quick Check:

    hashlib.md5() input = bytes [OK]
Hint: Always encode strings to bytes before hashing [OK]
Common Mistakes:
  • Passing string directly without encoding
  • Thinking hexdigest() is missing
  • Assuming MD5 unsupported in hashlib
5. You want to securely store user passwords. Which approach is best considering hashing algorithms?
hard
A. Use SHA-256 with a unique salt for each password
B. Use MD5 without salt because it's faster
C. Store passwords in plain text for easy retrieval
D. Use SHA-1 without salt since it's widely supported

Solution

  1. Step 1: Understand password storage best practices

    Storing passwords securely requires hashing with a strong algorithm and adding a unique salt to prevent attacks like rainbow tables.
  2. Step 2: Evaluate options

    SHA-256 is stronger than MD5 and SHA-1. Using a unique salt per password improves security significantly.
  3. Final Answer:

    Use SHA-256 with a unique salt for each password -> Option A
  4. Quick Check:

    Strong hash + salt = secure password storage [OK]
Hint: Always salt and use strong hashes like SHA-256 [OK]
Common Mistakes:
  • Using MD5 without salt thinking it's secure
  • Storing passwords as plain text
  • Using outdated SHA-1 without salt