๐ JavaScript Web Cryptography API: Learn How Hashing a Password Works
Imagine this: You and your best friend want to share secrets during class (not that Iโm encouraging it ๐ ). You decide to write your message in code so that if someone else snatches the paper, they wonโt understand it.
That โcodeโ you make is basically encryption. And when your friend uses the โkeyโ to read it back into normal words, thatโs decryption.
Now, the internet works the same way! When you log in to your bank app, or when you send a WhatsApp message, those apps donโt just send your plain password or text across the internet. That would be dangerous. Instead, they use cryptography โ scrambling and protecting information with special math.
The Web Cryptography API is a set of tools inside your browser (JavaScript) that lets websites do this encryption, decryption, signing, and verifying securely โ right on your device.
๐ What can the Web Crypto API do?
Think of it like a toolbox for security. The main tools are:
- Generate Keys โ Make secret codes (keys) for encrypting and decrypting.
- Encrypt & Decrypt Data โ Scramble info before sending; unscramble it when received.
- Hashing โ Create a fingerprint of data (used for passwords).
- Digital Signatures โ Prove that a message really came from you.
- Random Numbers โ Create strong, unpredictable values for security (not simple โMath.random()โ).
โก Example โ Generating Random Numbers
const array = new Uint32Array(5);
window.crypto.getRandomValues(array);
console.log(array);
๐ This makes 5 strong random numbers that can be used for passwords or keys.
โก Example โ Hashing a Password
Hashing is like turning your password into a fingerprint that canโt easily be reversed.
async function hashPassword(password) {
const encoder = new TextEncoder();
const data = encoder.encode(password);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
}
hashPassword("mypassword123").then(console.log);
๐ If you type "mypassword123"
, youโll get a long scrambled string like
ef92b...
(a hash). Thatโs what gets stored, not your real password.
โก Example โ Encrypt and Decrypt
Websites can lock (encrypt) your data and unlock (decrypt) it only if they have the right key.
// Simplified idea: Using AES (Advanced Encryption Standard)
const key = await crypto.subtle.generateKey(
{ name: "AES-GCM", length: 256 },
true,
["encrypt", "decrypt"]
);
Once you have a key, you can encrypt a message before sending it. Only someone with the same key can decrypt it.
๐ฎ Real-Life
- WhatsApp / Telegram: Messages are end-to-end encrypted โ nobody (not even the company) can read them except you and your friend.
- Online Games: Your login info is encrypted so hackers canโt steal your password.
- School Portal: When you submit homework online, your login details and grades are protected.
โ ๏ธ Important Notes
- The Web Crypto API is built into modern browsers โ you donโt need extra libraries.
- It is much safer than using normal
Math.random()
or writing your own crypto code. - But it can be a bit complex, so usually web developers use higher-level libraries that rely on Web Crypto behind the scenes.
โ Summary
- The Web Cryptography API is like a secret toolbox in your browser for protecting information.
- It can encrypt, decrypt, hash, sign, and generate secure random numbers.
- It makes websites safer for things like messages, passwords, and payments.
- Think of it as the reason why your WhatsApp messages stay private and your online banking is safe.
๐ Review โ Fill in the Gaps
- Cryptography is about hiding information so only someone with the right ______ can read it.
- The Web Crypto API lets JavaScript do security work _______ in the browser.
- A password is usually stored as a ______, not as plain text.
- The method
crypto.getRandomValues()
generates strong ______ numbers. - The function
crypto.subtle.digest("SHA-256", data)
is used to create a ______ of data. - Encrypting a message scrambles it, while ______ makes it readable again.
- WhatsApp uses ______ -to- ______ encryption to keep chats private.
- The Web Crypto API provides tools for generating keys, encrypting data, and making digital ______.
- Using
Math.random()
for security is bad because it is not truly ______. - The Web Crypto API helps keep passwords, payments, and messages ______.