liveBook · Manning

Password hashing

As explained in Just Use Postgres!

A password hash is a one-way derived value representing a password, typically incorporating the salt needed for later verification.

A password hash stores a derived representation instead of the raw password. In PostgreSQL, pgcrypto combines the password with a randomly generated Blowfish salt; the resulting value includes the salt for later verification. section 7.2.2

Store and verify a password

Create an accounts table with a password_hash column, then insert a password using crypt('SuperSecret123', gen_salt('bf')). To authenticate, compare password_hash with crypt('SuperSecret123', password_hash). The correct password returns ahamilton; an incorrect password returns zero rows. section 7.2.2

Listing 7.3 Encrypting the password for a new user
INSERT INTO accounts (username, password_hash)
VALUES ('ahamilton', crypt('SuperSecret123', gen_salt('bf')));

See the insert statement that generates a random salt and stores the resulting password hash. section 7.2.2

Practical properties:

  • Random salts produce different hashes for identical passwords and limit the impact of an exposed salt. section 7.2.2
  • Verification reuses the stored hash as crypt’s salt, so the raw password need not be stored. section 7.2.2

How Just Use Postgres! covers it

How deep it goes
Explains random salting, Blowfish hashing, embedded salts, and verification queries, but stops short of broader security administration.
Examples
Uses a PostgreSQL accounts table, the ahamilton account, SuperSecret123, and correct-versus-incorrect authentication queries.
What it assumes
Assumes basic PostgreSQL and SQL knowledge, while demonstrating extension installation and account-table setup.
What you can do after
Operate/configure pgcrypto to store password hashes and authenticate users with SQL.
How it connects
Connects password hashes with pgcrypto, crypt(), gen_salt(), salts, and password verification.

Also explained in

Compare how 5 books explain password hashing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.