liveBook · Manning

Gen_salt

As explained in Just Use Postgres!

gen_salt is a pgcrypto function that generates a random salt for password hashing using a specified algorithm.

gen_salt() generates a random salt for password hashing with a specified algorithm. With Blowfish, call it for every new password so identical passwords produce different hashes; the resulting hash retains the salt for later verification. section 7.2.2

Salt a new password with Blowfish

INSERT INTO accounts (username, password_hash) VALUES ('ahamilton', crypt('SuperSecret123', gen_salt('bf'))); The generated password_hash contains the salt. During login, crypt('SuperSecret123', password_hash) reuses that stored salt and returns a matching hash.

How Just Use Postgres! covers it

How deep it goes
Explains algorithm selection, per-password invocation, salt reuse during verification, and the security rationale for avoiding fixed salts.
Examples
Demonstrates crypt('SuperSecret123', gen_salt('bf')) when inserting a user password.
What it assumes
Assumes the pgcrypto extension is enabled and the accounts table stores password hashes.
What you can do after
Apply it via PostgreSQL pgcrypto to create salted password hashes for new accounts.
How it connects
Ties gen_salt to pgcrypto, crypt(), password hashing, and random salting.

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.