惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
Y
Y Combinator Blog
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog
M
MIT News - Artificial intelligence
Last Week in AI
Last Week in AI
V
V2EX
腾讯CDC
F
Fortinet All Blogs
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How We Handle Encrypted Database Fields in a Next.js App
Shivam · 2026-05-19 · via DEV Community

When we started building Fledgr, we knew pretty early that some data needed stronger protection than a locked-down database alone could provide.

Things like:

  • UPI IDs
  • Placement package figures
  • Anonymous post content

If a database dump ever leaked, we didn’t want sensitive information sitting there in readable form.

That led us to implement field-level encryption.


The Core Idea

Instead of using one static encryption key everywhere, we derive a unique key per field using HKDF.

That means the encryption key for a user's UPI ID is cryptographically separate from the key used for placement data or anonymous content — even though they all originate from the same master secret.

Every sensitive field gets its own context string:

db:users:upiId
db:placements:package
anon:posts:2025-01-15

Enter fullscreen mode Exit fullscreen mode

The nice part is that we never store these derived keys. They’re generated only when needed.


Encryption Flow

export function encrypt(value: string, context: string): string {
  const key = deriveKey(context);
  const iv = randomBytes(12);

  const cipher = createCipheriv("aes-256-gcm", key, iv);

  const encrypted = Buffer.concat([
    cipher.update(value, "utf8"),
    cipher.final(),
  ]);

  const tag = cipher.getAuthTag();

  return Buffer.concat([iv, tag, encrypted]).toString("base64");
}

Enter fullscreen mode Exit fullscreen mode

We use:

  • AES-256-GCM for authenticated encryption
  • Random 12-byte IVs
  • HKDF-derived contextual keys

This keeps encryption isolated across different parts of the system.


Handling Old Plaintext Data

Rolling out encryption in production usually means dealing with legacy rows that were stored before encryption existed.

We didn’t want migrations causing crashes or corrupting data, so every decrypt operation goes through a safe wrapper.

If decryption fails, we simply return the original value instead of throwing an error.

That allowed us to gradually migrate older data without downtime or breaking existing records.


Querying Encrypted Fields

One challenge with encrypted data is querying it.

You can’t directly run WHERE email = ... against encrypted values because encryption output changes every time.

To solve that, we use blind indexes.

For searchable fields like college emails, we store:

  • The encrypted value
  • A separate HMAC-based hash column

All lookup queries run against the hash instead of the encrypted field itself.

email_hash = HMAC_SHA256(email)

Enter fullscreen mode Exit fullscreen mode

The actual encrypted value is never used inside query conditions.


What This Actually Protects Against

Field-level encryption is not a replacement for proper secret management.

If the master secret leaks, everything downstream is compromised too.

But that wasn’t the primary threat model we were solving for.

The goal was protecting against database-level exposure:

  • Misconfigured backups
  • Leaked database dumps
  • Overprivileged read replicas
  • Snapshot exposure
  • Internal read-only access gone wrong

Those are realistic risks for modern applications, and field-level encryption helps reduce the blast radius significantly.


Performance Impact

We’ve been running this setup in production at Fledgr with almost no noticeable overhead.

The derive-on-demand approach is lightweight enough that users never feel it, while still giving us much stronger isolation for sensitive data.

For us, it ended up being one of those rare security improvements that added meaningful protection without making development harder.