Week 10

Security Mindset

Threat Modeling Basics

Common Backend Vulnerabilities

Handling Sensitive Data

Hashing vs. Encryption

Authentication & Authorization

Spring Security

Practice

Assignment

Back end Track

Under construction

<aside> 🚧

This page is currently under construction. Please check back later.

</aside>

Introduction

In an era of constant cyber threats and data breaches, robust application security relies on cryptographic techniques and tools to ensure both confidentiality and integrity in compliance with regulatory standards. People often mixing the terms "hashing" and "encryption" when talking about cryptography, but they are fundamentally different and serve distinct purposes. Choosing the wrong one can lead to a major security vulnerability.

Data Encryption?

Data encryption is a security process that converts readable data into an unreadable code, known as cipher-text, using a specific algorithm and a secret key. This ensures that only authorized individuals with the correct key can decipher the information, protecting sensitive data from unauthorized access, both when it is being stored and when it is being transmitted.

How does it work:

Data encryption works by applying a mathematical algorithm, along with a secret key, to transform the original plaintext data into an unreadable ciphertext. The same key is then used to then reverse the process, “decrypting the ciphertext,” and revealing the original plaintext. Without the correct key, the ciphertext remains unintelligible and thus inaccessible to unauthorized individuals.

Types of Data Encryption

There are two broad categories of data encryption:

The type of encryption chosen depends on the specific needs and resources of the situation.

Common Data Encryption Algorithms

Data encryption algorithms convert plaintext data into ciphertext, ensuring data confidentiality and integrity. Each method has a compelling use case and times when it is not sufficient. Before implementing one into your business, consider how each might benefit or hinder your data privacy goals.

Benefits of Data Encryption

Challenges in Implementing Data Encryption

Implementing data encryption involves three main challenges. First, complex key management is required to securely generate, store, and rotate cryptographic keys, as improper handling can make the encryption worthless. Second, encryption can create a performance impact, slowing down systems and forcing a trade-off between security and application speed. Finally, organizations face the difficulty of ensuring compliance with diverse and evolving industry and regional data protection regulations.

Hashing

Hashing is a one-way cryptographic process that transforms any input data into a fixed-size string of characters, known as a hash value or digest. Unlike encryption, which is a two-way process designed to be reversed (decrypted), hashing is irreversible. You cannot retrieve the original data from its hash. The primary purpose of hashing is not to hide data, but to verify its integrity and authenticity. By comparing the hash of a piece of data at two different points in time, you can instantly know if it has been altered in any way.

How does it work?

Hashing works by feeding data of any size—from a single word to a large file—into a mathematical hash function. This function processes the input and produces a unique, fixed-length output. For example, the SHA-256 algorithm will always produce a 256-bit (64-character) hash, regardless of whether the input is "hello" or the entire text of a book.

The process is deterministic, meaning the same input will always generate the exact same hash value. However, a tiny change in the input (like changing a single letter) will produce a completely different hash.

Key Properties of a Good Hash Function

A secure hashing algorithm is built on several key principles:

Common Hashing Algorithms

Just like with encryption, different hashing algorithms have been developed over the years, with varying levels of security.

Where is Hashing Used?

Hashing is a workhorse in modern computing and is used in many scenarios:

Encryption vs. Hashing: A Quick Summary

Feature Encryption Hashing
Purpose Confidentiality (to keep data secret) Integrity (to verify data hasn't changed)
Function Type Two-way (Encrypt & Decrypt) One-way (Irreversible)
Output Variable length (related to input) Fixed length
Key Requires a secret key to reverse No key is used
Primary Use Case Securing data in transit and at rest Storing passwords, verifying file integrity

Now that we understand the fundamental difference and know that hashing is the correct tool for passwords, let's explore how to do it correctly.

Secure Password Storage with bcrypt

We know now we must hash passwords, not encrypt them, the next question is: how do we hash them securely?

You will think let’s just use a standard hashing algorithm like SHA-256 to hash the password and store it in the database. It's one-way, so it's secure, right? Well, not so fast. There are so many lists nowadays of the 10 million most common passwords in the internet. Attackers can calculate the SHA-256 hash for all of them and stored them in a giant lookup table “called a rainbow table”. Then they take the hashes from the compromised database, look them up in their table, and instantly find the original password for thousands of users.

Simple Hashing is Not Enough

A simple hash is deterministic: the same input always produces the same output. hash("password123") will always result in the same hash value. This is what allows attackers to use pre-computed rainbow tables to crack passwords in seconds. So what we can do? Salt and Slowing Down

To defeat rainbow tables, we introduce randomness.

  1. Salting: A salt is a random string of data that is unique to each user. We append this salt to the password before hashing it.

  2. Slowing Down: Attackers can still try to crack one password at a time (a "brute-force" attack). To make this impractical, we should use a hashing algorithm that is intentionally slow. If it takes a fraction of a second to check one password, trying billions of combinations becomes impossibly expensive for the attacker.

The Standard - bcrypt**:**

Fortunately, we don't have to build this ourselves. We use algorithms designed for this purpose. The industry standard is bcrypt.

Why bcrypt is the right tool:

Practical Implementation in Spring Boot