pg_pwhash
PostgreSQL 高级密码哈希扩展(Argon2/scrypt/yescrypt)
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
pg_pwhash | 1.0 | SEC | MIT | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 7330 | pg_pwhash | 否 | 是 | 否 | 是 | 否 | 是 | - |
RPM metadata shows license=PostgreSQL, but packaged LICENSE file is MIT
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PGDG | 1.0 | 1817161514 | pg_pwhash | - |
| RPM | PGDG | 1.0 | 1817161514 | pg_pwhash_$v | - |
| DEB | PGDG | 1.0 | 1817161514 | postgresql-$v-pg-pwhash | - |
安装
您可以直接安装 pg_pwhash 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:
pig repo add pgdg -u # 添加 PGDG 仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install pg_pwhash; # 当前活跃 PG 版本安装
pig ext install -y pg_pwhash -v 18 # PG 18
pig ext install -y pg_pwhash -v 17 # PG 17
pig ext install -y pg_pwhash -v 16 # PG 16
pig ext install -y pg_pwhash -v 15 # PG 15
pig ext install -y pg_pwhash -v 14 # PG 14
dnf install -y pg_pwhash_18 # PG 18
dnf install -y pg_pwhash_17 # PG 17
dnf install -y pg_pwhash_16 # PG 16
dnf install -y pg_pwhash_15 # PG 15
dnf install -y pg_pwhash_14 # PG 14
apt install -y postgresql-18-pg-pwhash # PG 18
apt install -y postgresql-17-pg-pwhash # PG 17
apt install -y postgresql-16-pg-pwhash # PG 16
apt install -y postgresql-15-pg-pwhash # PG 15
apt install -y postgresql-14-pg-pwhash # PG 14
创建扩展:
CREATE EXTENSION pg_pwhash;
用法
pg_pwhash 为 PostgreSQL 提供现代自适应密码哈希算法,包括 Argon2、scrypt 和 yescrypt。
CREATE EXTENSION pg_pwhash;
支持的算法
| 标识符 | 算法 | Salt 模式 |
|---|---|---|
argon2i | Argon2i | $argon2i$v=19$m=4096,t=3,p=1$<salt> |
argon2d | Argon2d | $argon2d$v=19$m=4096,t=3,p=1$<salt> |
argon2id | Argon2id | $argon2id$v=19$m=4096,t=3,p=1$<salt> |
scrypt | Scrypt | $scrypt$ln=16,r=8,p=1$<salt> |
$7$ | Scrypt (crypt) | $7$BU<salt> |
yescrypt | yescrypt (crypt) | $y$j9T$<salt> |
核心函数
生成盐值和哈希
-- Argon2id(推荐)
SELECT pwhash_crypt('password', pwhash_gen_salt('argon2id'));
-- $argon2id$v=19$m=4096,t=3,p=1$<salt>$<hash>
-- Scrypt
SELECT pwhash_crypt('password', pwhash_gen_salt('scrypt'));
-- Yescrypt
SELECT pwhash_crypt('password', pwhash_gen_salt('yescrypt'));
验证密码
-- 如果输出等于存储的哈希值则匹配
SELECT stored_hash = pwhash_crypt('entered_password', stored_hash) AS valid;
直接哈希函数
SELECT pwhash_argon2('password', pwhash_gen_salt('argon2id'));
SELECT pwhash_scrypt('password', pwhash_gen_salt('scrypt'));
SELECT pwhash_yescrypt_crypt('password', pwhash_gen_salt('yescrypt'));
自定义盐值参数
-- 自定义内存/时间/并行度的 Argon2
SELECT pwhash_gen_salt('argon2id', 'm=65536', 't=4', 'p=2');
-- 自定义参数的 Scrypt
SELECT pwhash_gen_salt('scrypt', 'ln=20', 'r=8', 'p=1');
配置
| 参数 | 描述 |
|---|---|
pg_pwhash.argon2_default_backend | Argon2 后端:libargon2 或 openssl |