PII Hashing

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up pii hashing by running a one-way hash function on sensitive fields. This turns raw values like emails or phone numbers into fixed strings. Most teams run this step inside a data pipeline before loading tables. Still, broad database roles still let users read raw tables in production. How pii hashing works in your database setup A hash function takes an input string and makes a fixed-length output. Given the same input, the math always makes the exact same output. Teams use this fea

PII in Logs Prevention: the complete guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free white paper. No spam. Unsubscribe anytime.

You set up pii hashing by running a one-way hash function on sensitive fields. This turns raw values like emails or phone numbers into fixed strings. Most teams run this step inside a data pipeline before loading tables. Still, broad database roles still let users read raw tables in production.

How pii hashing works in your database setup

A hash function takes an input string and makes a fixed-length output. Given the same input, the math always makes the exact same output. Teams use this feature to join tables without storing clear text values. You can match a user across two tables using only the hash.

Basic hashing has a big flaw when inputs follow a known pattern. Phone numbers, card numbers, and national IDs have small, predictable value ranges. An attacker can hash every possible phone number in a few minutes. Then they compare their list against your database to find the matches.

To fix this flaw, teams add a secret string called a salt. You join the salt with the email or phone number before hashing. If you use a secret key with the hash, that is an HMAC. The database or app must store that secret key in a safe place. If a user gets the key, they can still guess the inputs.

Most teams build hashing rules into their data load jobs or views. The job reads from the live database and writes hashes to analytics tables. Analysts then query the analytics tables using their standard database roles.

What native hashing solves and what it leaves open

Hashing in a pipeline protects copies of data in your warehouse tables. It lets analysts count distinct users or join records without clear text. If someone exports an analytics table, the file holds only hashed strings.

Still, this setup does not help with live production databases at all. Your core apps need real emails and phone numbers to work right. You cannot replace live table columns with one-way hashes in production. If you hash an email in place, your app cannot send messages.

PII in Logs Prevention: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Because the live data stays in clear text, support work gets risky. Engineers and support staff must log in to check bugs and fix rows. Native database roles decide who can log in and read a table. Once a role grants read rights, every query returns the raw columns.

Database views can hash columns on the fly for specific read roles. Yet views take extra work to maintain as schemas change over time. One missed view grant or broad role exposes raw card numbers again. Static database permissions cannot inspect individual queries or rewrite results in flight.

When to hash data versus masking it in results

Teams often mix up one-way hashing with data masking during setup. Hashing changes data forever so you can link records across different tables. It works best for warehouse jobs where no one needs the original value.

Masking is a better fit when people query live databases for support. When an engineer runs a query, masking rewrites matched fields in results. Emails, phone numbers, card numbers, and national IDs get replaced before display. The raw data stays intact on disk so the app still runs.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Can you reverse a hash to get the original value back?

No, a true hash function works in one direction only. Still, attackers can guess short inputs like phone numbers by testing every option. Adding a strong secret salt stops people from guessing common inputs.

Does pii hashing remove the need for database access controls?

No, hashed data still requires strict role rules and query logs. Unsalted hashes can leak data through simple guessing attacks. Also, live app tables still hold raw data that needs strong protection.

Should we hash emails and phone numbers in our main app database?

You should not replace raw contact fields with hashes in live tables. Your app needs the real email or phone number to contact users. Keep raw values in the live table and rewrite matched fields in results.

Save the open-source gateway.

hoop is MIT-licensed infrastructure for controlling how AI agents reach production data. Star hoophq/hoop so you can inspect it, deploy it, or share it when your team starts governing agent access.