L
LLLOS.ai
Learn
L

Ask Your Tutor · Computer Science

How does a hash function work inside a computer?

From NCERT In depth Asked by Anonymous from Seattle, United States 23 Oct 2026, 5:21 PM IST Updated

In one line

A hash function turns any input (like a word or file) into a fixed-size string of numbers and letters, in a way that is fast and hard to reverse.

SHA-2 — Set of cryptographic hash functions
SHA-2 — Set of cryptographic hash functions
SHA-2, a famous family of hash functions used in security and digital signatures.
Image: Wikipedia

The rule

A hash function is a process that takes an input (called a key) and returns a fixed-size string of characters, which is typically a sequence of numbers and letters, called the hash value or hash code.

There is no single formula for all hash functions, but a common example is:

hash = H(input)

Where:

  • H is the hash function (like MD5, SHA-1, SHA-256)
  • input is the data you want to hash
  • hash is the output (the hash value)

Let's understand it simply

Imagine you are at a busy railway station. You want to keep your bag in a locker. There are 100 lockers, and you want a quick way to decide which locker to use. You think of a rule: "Take the first letter of my name, turn it into a number, and use that number as my locker number." So, if your name is Amit, you use locker 1 (A=1). If your name is Sita, you use locker 19 (S=19).

A hash function works like this rule. It takes any input (your name, a file, a password) and quickly turns it into a number (the locker number). This number tells the computer where to store or find your data. The rule is always the same, so the same input always gives the same output.

Hashing vs Encryption
HashingEncryption
PurposeQuick lookup, data integrityData secrecy, privacy
Can you reverse it?NoYes (with key)
Output sizeFixedCan vary
Example usePasswords, file checksMessages, bank data

How it works

  1. Input is given: You give the computer some data (like a password or file).
  2. Hash function applies a rule: The computer runs a set of steps (the hash function) on your data. This usually involves math, mixing, and rearranging bits (the smallest pieces of data).
  3. Output is a fixed-size hash: No matter how big or small your input is, the output is always the same size (for example, 256 bits for SHA-256).
  4. Small changes, big difference: Even if you change just one letter in your input, the hash value will change completely.
  5. One-way process: You cannot easily go backward from the hash to the original input. It is like making chutney from vegetables—you cannot get the vegetables back from the chutney!
  6. Used for quick lookup: The computer uses the hash value to quickly find, check, or store data, like finding your bag in the right locker.
How a hash function works
  1. Input data is given (like a password)
  2. Hash function applies math rules
  3. Output is a fixed-size hash value
  4. Hash is used for lookup or checking

The story behind it

Hash functions started becoming popular in the 1950s and 1960s, when computers needed fast ways to store and find data in large lists (like phone books). The idea was to avoid searching through every item. Instead, a hash function could jump straight to the right spot. Later, hash functions became very important in security (like passwords and digital signatures). One famous hash function is SHA-256, made by the US government in 2001. It is still used today in things like WhatsApp messages and Bitcoin.

Where you see it in real life

  • Passwords: When you set a password, the computer stores only the hash, not your real password. When you log in, it hashes what you type and checks if it matches.
  • Aadhaar data: To keep your details safe, Aadhaar uses hash functions to protect your information.
  • WhatsApp messages: Hash functions help check if a message was changed on the way.
  • Banking apps: Hashes make sure your transaction details are not tampered with.
  • Cricket score websites: Hashes help quickly find the right match details from a big database.
  • Checking file downloads: When you download a file, the website gives you a hash. You can check if your file's hash matches, to see if it is safe and unchanged.

Why it matters

  • Security: Hash functions keep passwords and data safe, even if someone steals the database.
  • Speed: Computers use hashes to find data quickly, like searching for a friend's phone number in a huge list.
  • Data integrity: Hashes help check if a file or message has been changed or damaged.
  • Digital signatures: Used in online forms, contracts, and payments to prove something is genuine.
  • Blockchain and cryptocurrency: Bitcoin and other cryptocurrencies use hash functions to keep records secure.

Worked example

Let's see a simple hash function (not used in real life, just to show the idea): Suppose our hash rule is: Add up the numbers for each letter (A=1, B=2, ..., Z=26), then take the last digit.

Given: Input = "RAM"
Step 1: R = 18, A = 1, M = 13
Step 2: Add them: 18 + 1 + 13 = 32
Step 3: Take the last digit: 2
Step 4: Hash value = 2

So, "RAM" hashes to 2 using this simple rule. Real hash functions use much more complex math and give longer outputs.

Common mistakes

  • Thinking hashes are encryption: Hashing is not the same as locking data with a key. Hashes cannot be turned back into the original data.
  • Believing two different inputs never have the same hash: Sometimes, two different inputs can give the same hash (called a collision), but good hash functions make this very rare.
  • Using weak hash functions for passwords: Old hash functions like MD5 are not safe for passwords anymore. Always use strong ones like SHA-256 or bcrypt.
  • Forgetting that hash outputs are fixed size: No matter how big the input, the hash is always the same length.

Remember it

A hash function is like a magic box: put anything in, get a fixed-size code out, but you can never get the original thing back from the code.

Check yourself

Why can't you get your original password back from its hash?

Because hashing is a one-way process; the hash does not store the original data.

What happens if you change just one letter in your input to a hash function?

The hash value changes completely, making it hard to guess the original input.

Name one place where hash functions are used in daily life.

Passwords—computers store only the hash, not your real password.

Is hashing the same as encryption? Why or why not?

No, encryption can be reversed with a key, but hashing cannot be reversed.

Quick recap

  • A hash function turns any input into a fixed-size code.
  • It is fast, one-way, and hard to reverse.
  • Hashing is used for passwords, security, and quick searches.
  • Even a small change in input gives a very different hash.
  • Hashing is not the same as encryption.
Written by the LLOS.ai tutor, checked against NCERT chapters. Asked by Anonymous from Seattle, United States on 23 Oct 2026, 5:21 PM IST