What if your password could be checked without ever being seen by anyone?
Why Hashing algorithms (SHA, MD5) in Cybersecurity? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a huge list of passwords written on paper, and you need to check if a new password is already used without revealing the original passwords.
Manually comparing each password is slow and risky because anyone who sees the list can steal all passwords. Also, mistakes can happen easily when handling many entries.
Hashing algorithms like SHA and MD5 turn passwords into unique codes that hide the original data. This way, you can check if passwords match without ever seeing the real ones.
if new_password == stored_password:
allow_access()if hash(new_password) == stored_hash:
allow_access()It enables secure and fast verification of data without exposing sensitive information.
When you log into a website, your password is hashed and compared to the stored hash, keeping your actual password secret even if the database is leaked.
Manual password checks risk exposure and errors.
Hashing hides original data while allowing verification.
SHA and MD5 are common algorithms used for this purpose.
Practice
MD5 and SHA in cybersecurity?Solution
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.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.Final Answer:
To create a fixed-size code representing data for integrity verification -> Option BQuick Check:
Hashing = Data integrity check [OK]
- Confusing hashing with encryption
- Thinking hashing compresses data
- Believing hashing generates passwords
Solution
Step 1: Recall MD5 hash size
MD5 produces a fixed-size hash of 128 bits, usually shown as a 32-character hexadecimal string.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.Final Answer:
A 128-bit fixed-length hexadecimal string -> Option DQuick Check:
MD5 hash length = 128 bits [OK]
- Thinking MD5 output length varies
- Confusing MD5 with SHA-256 length
- Assuming output is plain text
import hashlib hash_object = hashlib.sha256(b'password123') hex_dig = hash_object.hexdigest() print(hex_dig)
What will this code output?
Solution
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.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.Final Answer:
A 64-character hexadecimal string representing the SHA-256 hash of 'password123' -> Option AQuick Check:
SHA-256 output length = 64 hex chars [OK]
- Expecting original string as output
- Confusing input type causing error
- Mixing MD5 and SHA-256 output lengths
import hashlib password = 'mypassword' hash = hashlib.md5(password) print(hash.hexdigest())
What is the error in this code?
Solution
Step 1: Check input type for hashlib.md5()
hashlib.md5() requires a bytes-like object, but the code passes a string directly.Step 2: Identify correct fix
The string should be encoded to bytes using password.encode() before passing to md5().Final Answer:
The input to md5() must be bytes, not a string -> Option CQuick Check:
hashlib.md5() input = bytes [OK]
- Passing string directly without encoding
- Thinking hexdigest() is missing
- Assuming MD5 unsupported in hashlib
Solution
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.Step 2: Evaluate options
SHA-256 is stronger than MD5 and SHA-1. Using a unique salt per password improves security significantly.Final Answer:
Use SHA-256 with a unique salt for each password -> Option AQuick Check:
Strong hash + salt = secure password storage [OK]
- Using MD5 without salt thinking it's secure
- Storing passwords as plain text
- Using outdated SHA-1 without salt
