Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 55 ยท Security and Care

Passwords, Hashing and Encryption

Learn what makes a password strong, how hashing differs from encryption, how symmetric and public-key cryptography work, and why 2FA and HTTPS matter.

Almost everything private about your digital life is protected by a secret: a password, a key, a code sent to your phone. Understanding how these protections work helps you pick good ones, and also explains why advice such as "use a password manager" or "turn on two-factor authentication" is not just fussiness. Security is mostly about making attacks expensive, so that guessing or breaking in costs far more than it is worth.

This lesson covers strong passwords and managers, the difference between hashing and encryption, the two families of encryption (symmetric and public-key), two-factor authentication and HTTPS. The real technology is used by every website and phone; here you will use Node's built-in crypto module as an honest stand-in to see the ideas working on small examples. It follows How the Web Works and Email, the Cloud and Everyday Online Services.

What makes a password strong

An attacker rarely types guesses by hand. Programs try millions or billions of candidates per second, starting with common passwords, then words with small changes, then everything else. A password is strong when it is long and unpredictable.

Strength can be measured in bits of entropy: if a password is chosen at random from N equally likely possibilities, it carries log2(N) bits, and each extra bit doubles the attacker's work. For a random string, entropy is length * log2(pool size). The two key words are random and length. P@ssw0rd! looks complicated, but it is a famous pattern that appears in every cracking list, so its real strength is tiny. A password you choose by feel is far weaker than the same-looking string chosen by a computer.

The stand-in below estimates how long an exhaustive search would take, assuming a fast attacker who can try ten billion guesses per second against a weakly protected (fast-hash) password database.

Node.js Entropy and guessing time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Two lessons stand out. Length beats cleverness: six random words from a list of 7,776 are about as strong as twelve random characters, and much easier to remember. And the numbers apply only to random choices; a birthday, a pet's name or a keyboard pattern has far fewer real possibilities than its length suggests.

Password managers

Nobody can remember dozens of long random passwords. A password manager is an app that stores them in an encrypted vault, protected by one strong master password (a passphrase of several random words works well), and fills them in for you. That makes it practical to follow the two most important rules:

  1. Use a different password for every account. Websites do get breached. If you reuse a password, attackers try the leaked pair on other sites (a "credential stuffing" attack), and one leak becomes ten.
  2. Let the manager generate long random passwords, and only memorize the master passphrase.

Forcing frequent changes of a good password helps little. Change a password when you suspect it was exposed, not on a calendar.

Hashing: a one-way fingerprint

A hash function turns any input into a fixed-size fingerprint, called a hash or digest. A good hash function has these properties:

  • the same input always gives the same output;
  • a tiny change in the input changes the output completely;
  • the output has a fixed length, whatever the input size;
  • it is practically impossible to go backwards from the hash to the input.

Hashing is not encryption. Encryption is designed to be reversed by someone with the key; a hash is designed to be a one-way street. Hashes are used to check that a downloaded file is unchanged, to find duplicate files, and, most importantly, to store passwords.

A well-run website never stores your password. It stores a hash, and when you sign in it hashes what you typed and compares. If the database leaks, the attacker gets hashes, not passwords.

Node.js SHA-256: same input, same fingerprint

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

One character of difference, and the whole fingerprint changes. Yet attackers can still use hashes, because people choose common passwords. They hash a list of likely passwords once (a "lookup table") and compare it with the stolen hashes. Two defenses stop this:

  • A salt is a random value stored next to each user's hash and mixed into the hashing. Two users with the same password then get different hashes, and a precomputed table becomes useless.
  • A slow, purpose-built password hash (such as scrypt, bcrypt or Argon2) takes a noticeable fraction of a second and a lot of memory on purpose. That is invisible to one honest login but makes billions of guesses per second impossible. A plain fast hash such as SHA-256 is the wrong tool for passwords.
Node.js A leaked table, with and without salt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Encryption: secrets you can get back

Encryption scrambles data with a key so that only someone holding the right key can turn it back into readable form. The original is called plaintext and the scrambled version ciphertext. Good algorithms are public; only the key is secret. There are two families.

Symmetric encryption

In symmetric encryption, one key both encrypts and decrypts. It is fast and used for bulk data: encrypted disks, backups, the video call itself. Modern standard: AES (the Advanced Encryption Standard) with 128- or 256-bit keys. The hard part is the key distribution problem: how do two people agree on a key without an eavesdropper learning it?

Modes like AES-GCM also add an authentication tag, so that if anyone alters the ciphertext, or the wrong key is used, decryption fails loudly instead of returning garbage.

Node.js AES-256-GCM: encrypt, decrypt, fail

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Public-key encryption

Public-key (asymmetric) cryptography solves the distribution problem with a pair of mathematically linked keys. The public key can be shared with everyone; the private key never leaves its owner.

  • Anything encrypted with the public key can only be decrypted with the private key, so anyone can send you a secret.
  • A digital signature works the other way: you sign with the private key, and anyone can check the signature with your public key. It proves who made the message and that it was not changed.

Public-key operations are slow, so real systems use them briefly, to agree on or deliver a fresh symmetric key, and then switch to fast symmetric encryption for the data. Common algorithms are RSA and elliptic-curve schemes.

flowchart LR
  A["Alice"] -->|"1. fetches Bob's<br/>public key"| B["Bob's public key"]
  A -->|"2. encrypts a session key<br/>with the public key"| C["Ciphertext"]
  C -->|"3. only Bob's private key<br/>can open it"| D["Bob"]
  D -.->|"4. both now use the session key<br/>for fast symmetric encryption"| A

Public-key cryptography delivers a shared secret safely; symmetric encryption then protects the conversation.

Node.js Public keys: encrypt, decrypt, sign, verify

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Two-factor authentication

A password is something you know. Two-factor authentication (2FA), also called multi-factor, adds a second kind of proof: something you have (a phone or hardware key) or something you are (a fingerprint or face). Even if a thief learns your password through a leak or a phishing page, they still lack the second factor.

Common second factors, from weakest to strongest in practice:

  • SMS codes: convenient, but phone numbers can be hijacked ("SIM swapping").
  • Authenticator apps generate a six-digit code that changes every 30 seconds from a shared secret and the current time.
  • Passkeys and security keys use public-key cryptography: the site stores only your public key, the private key stays in your device, and nothing secret is typed, so there is nothing to phish.

Turn 2FA on first for your email account, because email can reset all your other passwords.

HTTPS: encryption on the web

When the address begins with https://, your browser and the site first run a TLS handshake. In outline: the server shows a certificate, which is its public key signed by a trusted authority, proving that the key belongs to that site name. Then the two sides use public-key methods to agree on a fresh symmetric key and encrypt everything that follows. Someone on the same Wi-Fi network can see which site you contact, but not the pages, form fields or passwords you send. The padlock means the connection is encrypted and the site name matches; it does not mean the site is honest. Criminals can get certificates too, so see Malware, Phishing and Safe Habits.

What is real here and what is a stand-in? SHA-256, scrypt, AES-256-GCM, RSA-OAEP and signatures are real algorithms run by Node's crypto module (OpenSSL underneath). The examples are teaching-sized: real systems store a random salt and a per-message IV, rely on vetted libraries, and manage keys in secure hardware. Never invent your own cryptography.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which password is the strongest choice?

    Length and true randomness give entropy. Patterns that feel clever, such as P@ssw0rd!, appear in every cracking list.
  2. 2. How is hashing different from encryption?

    Encryption is designed to be undone by the key holder. A hash is a one-way fingerprint that cannot practically be turned back into the input.
  3. 3. Why do websites add a salt before hashing passwords?

    A unique random salt per user defeats lookup tables and makes the attacker work on each hash separately.
  4. 4. In public-key cryptography, which key do you keep secret?

    The public key is meant to be shared. The private key decrypts and signs, so it must never leave its owner.
  5. 5. What does the padlock in the browser address bar tell you?

    HTTPS protects the connection. It says nothing about whether the site's owner is honest, so phishing sites can have it too.

Summary

  • Strong passwords are long and random; entropy doubles with every added bit, and patterns that look clever are weak.
  • Use a password manager so each account has its own long random password, and protect it with one strong passphrase.
  • Hashing is a one-way fingerprint; sites store salted hashes made with slow password hashes (scrypt, bcrypt, Argon2), not passwords.
  • Symmetric encryption (AES) uses one shared key and is fast; public-key encryption uses a public and a private key and solves key sharing; signatures prove origin and integrity.
  • Two-factor authentication adds a second proof; passkeys and security keys resist phishing best. Protect your email account first.
  • HTTPS uses certificates and TLS to encrypt the connection, but the padlock does not prove a site is trustworthy.

Next, see how attackers trick people and programs: Malware, Phishing and Safe Habits.