Cryptography 101: Hashing & Encryption for Developers

Cryptography 101: Hashing & Encryption for Developers
1. Hardware-Mirror: The Physics of Entropy
If a cryptographic system is a fortress, Entropy is the foundation. If your "Random Number" is predictable, your encryption is a house of cards.
TRNG vs. PRNG: The Silicon Choice
- PRNG (Pseudo-Random): Software-based. If you know the "Seed" and the algorithm, you can predict every number that will ever be generated (Deterministic).
- TRNG (True-Random): Hardware-based. Modern CPUs (using the
RDRANDinstruction) tap into the Thermal Noise of the silicon itself. They measure the tiny, unpredictable voltage fluctuations caused by heat to produce "True" randomness.
Architecture Rule: For security keys, ALWAYS use the Hardware TRNG. In Node.js, this means using crypto.randomBytes(), which interfaces with the OS kernel's entropy pool (/dev/urandom). Never use Math.random(), which is a predictable PRNG designed for games, not security.
2. The Trinity: Hashing vs. Encryption vs. Encoding
The most common mistake in security is confusing these three distinct operations.
Encoding (The Format)
- Purpose: To represent data in a specific format (e.g., Base64, Hex).
- Security: Zero. Encoding is a public algorithm with no key.
- Rule: If there is no key, it is NOT security.
Hashing (The Fingerprint)
- Purpose: One-way transformation to verify integrity (e.g., SHA-256, Argon2).
- Properties: Deterministic, irreversible, and fixed-length.
- Use Case: Password storage (Argon2id) and file integrity.
Encryption (The Secret)
- Purpose: Two-way transformation to hide data from unauthorized viewers.
- Properties: Reversible (with a key).
- Use Case: Protecting data in transit (TLS) or at rest (Database encryption).
2. Symmetric Encryption: The Workhorse (AES-GCM)
Symmetric encryption uses the same key for both encryption and decryption. It is incredibly fast and efficient.
AES-GCM (Advanced Encryption Standard - Galois/Counter Mode)
In 2026, AES-GCM is the industry standard.
- Authenticated Encryption: Not only does it hide the data, but it also adds an "Authentication Tag" to ensure the data hasn't been tampered with.
- Hardware-Mirror Rule: Modern CPUs (Intel/AMD) have AES-NI (New Instructions). These are physical circuits dedicated only to running AES. This allows your server to encrypt gigabytes of data per second with almost zero CPU overhead.
3. Asymmetric Encryption: The Handshake (ECC & RSA)
Asymmetric encryption uses a Public Key to encrypt and a Private Key to decrypt. It is mathematically "Heavy" and slow.
why we need it:
You cannot securely send a Symmetric Key over the internet. So, you use Asymmetric Encryption to "wrap" the Symmetric Key. Once both sides have the same Symmetric Key, they switch to AES for the actual data transfer.
Architect's Choice: Use ECC (Elliptic Curve Cryptography) instead of RSA. ECC provides the same security as RSA but with much smaller keys (256-bit ECC = 3072-bit RSA). Smaller keys mean faster handshakes and less battery drain on mobile devices.
4. The Hardware Reality of Randomness
Cryptography is only as strong as its Initial Entropy. If your "Random Number" is predictable, your encryption is useless.
- PRNG (Pseudo-Random): Software-based. If you know the "Seed," you can predict the sequence.
- TRNG (True-Random): Hardware-based. Modern motherboards use Thermal Noise or Quantum Effects to generate truly unpredictable bits.
Developer Rule: Never use Math.random(). In Node.js or the browser, always use crypto.getRandomValues() or crypto.randomBytes(). These APIs tap directly into the hardware entropy pool.
3. Salt, Pepper, and Nonce Physics
Even with powerful hashing, your data is vulnerable to a Rainbow Table Attack. If two users have the same password "12345", their hashes will be identical.
The Salt (Per-Row Randomness)
- The Concept: You add a unique random string (the Salt) to every password before hashing.
- The Physics: This ensures that even if two users have the same password, their hashes on your disk look completely different. It forces the attacker to "Brute Force" every user individually, increasing their work by $1,000,000x$.
The Nonce (One-Time Use)
In symmetric encryption (AES-GCM), you use a Nonce (Number used once).
- The Risk: If you encrypt two different messages with the same key and the same nonce, a hacker can perform an "XOR Attack" to reveal the original text.
- The Rule: Never reuse a nonce with the same key. Period.
4. Key Management: The "Secret of the Secret"
Where do you store your encryption keys? This is the Root of Trust.
The KMS Paradox
If you store your database key on your server's disk, any hacker who gets "Read" access to your server now has your keys.
- The Solution: Key Management Service (KMS).
- The Internal Logic: Your server never sees the "Master Key." It sends to the KMS: "Please encrypt this data for me." The KMS (running on hardware-hardened HSMs) performs the operation and returns the ciphertext.
- Hardware-Mirror: High-security systems use FIPS 140-2 Level 3 hardware. If someone physically tries to open the HSM chip to steal the key, the device detects the pressure change and Electrically Destroys the key instantly.
5. Symmetric Encryption: The Workhorse (AES-GCM)
6. The Golden Rule: Don't Roll Your Own
Unless you have a PhD in Mathematics and ten years of peer-reviewed research, do not write your own crypto algorithms.
- Don't use "Creative" XOR patterns.
- Don't modify existing algorithms "to make them safer."
- Do use well-vetted libraries (like
libsodium,Tink, or the nativeWeb Crypto API).
Summary: Designing for the Unseen
Cryptography is the foundation of trust in a digital society. By understanding the difference between hashing and encryption, leveraging hardware-accelerated AES, and using modern primitives like ECC and Argon2, you build systems that can withstand the physical and mathematical pressure of the modern internet.
You are no longer just an architect of logic; you are a Guardian of Information.
6. Quantum-Resistance: The 2030 Horizon
Standard encryption (RSA and ECC) depends on the difficulty of factoring large numbers. Quantum Computers can solve this math in minutes.
The Transition to PQC (Post-Quantum Cryptography)
- The Reality: In 2026, we are beginning the migration to algorithms like Kyber and Dilithium.
- The Strategy: "Hybrid Encryption." You use both a classical ECC key and a Quantum-resistant key. If either one holds, your data remains secure.
Phase 2: Crypto Actions
- Audit your password storage. If you are using SHA-256 or MD5, plan an immediate migration to Argon2id.
- Implement AES-GCM for all data-at-rest encryption.
- Verify that your random number generation uses
crypto.randomBytes()or equivalent. - Plan your KMS Migration: Move your encryption keys out of
.envfiles and into a managed HSM-backed service.
Read next: IAM: Identity & Access Management Best Practices →
