# pgcrypto — Postgres extension

> Hashing, password hashing and encryption functions.

Source: https://scribase.com/extensions/pgcrypto

Category: Security and secrets. Version: 1.3. Enabled on every project by the platform.

## About

pgcrypto provides digest and hmac for hashing, crypt with gen_salt for bcrypt password hashes, gen_random_bytes for tokens, and PGP symmetric and public-key encryption. For random UUIDs you no longer need it: gen_random_uuid() is built into Postgres.

## Enable it

```sql
create extension if not exists pgcrypto with schema extensions;
```

## Example

```sql
select encode(digest('hello', 'sha256'), 'hex');

-- bcrypt hash and check
select crypt('correct horse', gen_salt('bf', 10));
select crypt('correct horse', stored_hash) = stored_hash from accounts where id = 1;

select encode(gen_random_bytes(32), 'base64') as token;
```

## Notes

- On in every project, in the extensions schema. Qualify calls as extensions.crypt(...) if that schema is not on your search_path.

Upstream: https://www.postgresql.org/docs/17/pgcrypto.html