跳转到主要内容

pgsmcrypto

为PostgreSQL提供商密算法支持:SM2,SM3,SM4

pgsmcrypto · 为PostgreSQL提供商密算法支持:SM2,SM3,SM4

概览

ID扩展软件包版本类别许可证编程语言
7080pgsmcryptopgsmcrypto0.1.1SECMITRust
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-d--否是否是否否

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.1.119 18 17 16 15pgsmcrypto-
RPMPIGSTY0.1.119 18 17 16 15pgsmcrypto_$v-
DEBPIGSTY0.1.119 18 17 16 15postgresql-$v-pgsmcrypto-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el8.aarch64N/APIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el9.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el9.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el10.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el10.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
d12.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
d12.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
d13.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
d13.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u22.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u22.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u24.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u24.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u26.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u26.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
软件包版本平台仓库大小下载
pgsmcrypto_190.1.1el9.x86_64pigsty1.7 MiBpgsmcrypto_19-0.1.1-4PGSTY.el9.x86_64.rpm
pgsmcrypto_190.1.1el9.aarch64pigsty1.5 MiBpgsmcrypto_19-0.1.1-4PGSTY.el9.aarch64.rpm
pgsmcrypto_190.1.1el10.x86_64pigsty1.7 MiBpgsmcrypto_19-0.1.1-4PGSTY.el10.x86_64.rpm
pgsmcrypto_190.1.1el10.aarch64pigsty1.5 MiBpgsmcrypto_19-0.1.1-4PGSTY.el10.aarch64.rpm
postgresql-19-pgsmcrypto0.1.1d12.x86_64pigsty1.2 MiBpostgresql-19-pgsmcrypto_0.1.1-4PGSTY~bookworm_amd64.deb
postgresql-19-pgsmcrypto0.1.1d12.aarch64pigsty986.3 KiBpostgresql-19-pgsmcrypto_0.1.1-4PGSTY~bookworm_arm64.deb
postgresql-19-pgsmcrypto0.1.1d13.x86_64pigsty1.2 MiBpostgresql-19-pgsmcrypto_0.1.1-4PGSTY~trixie_amd64.deb
postgresql-19-pgsmcrypto0.1.1d13.aarch64pigsty988.7 KiBpostgresql-19-pgsmcrypto_0.1.1-4PGSTY~trixie_arm64.deb
postgresql-19-pgsmcrypto0.1.1u22.x86_64pigsty1.3 MiBpostgresql-19-pgsmcrypto_0.1.1-4PGSTY~jammy_amd64.deb
postgresql-19-pgsmcrypto0.1.1u22.aarch64pigsty1.2 MiBpostgresql-19-pgsmcrypto_0.1.1-4PGSTY~jammy_arm64.deb
postgresql-19-pgsmcrypto0.1.1u24.x86_64pigsty1.3 MiBpostgresql-19-pgsmcrypto_0.1.1-4PGSTY~noble_amd64.deb
postgresql-19-pgsmcrypto0.1.1u24.aarch64pigsty1.1 MiBpostgresql-19-pgsmcrypto_0.1.1-4PGSTY~noble_arm64.deb
postgresql-19-pgsmcrypto0.1.1u26.x86_64pigsty1.3 MiBpostgresql-19-pgsmcrypto_0.1.1-4PGSTY~resolute_amd64.deb
postgresql-19-pgsmcrypto0.1.1u26.aarch64pigsty1.1 MiBpostgresql-19-pgsmcrypto_0.1.1-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pgsmcrypto_180.1.1el8.x86_64pigsty1.5 MiBpgsmcrypto_18-0.1.1-3PIGSTY.el8.x86_64.rpm
pgsmcrypto_180.1.1el8.aarch64pigsty1.4 MiBpgsmcrypto_18-0.1.1-3PIGSTY.el8.aarch64.rpm
pgsmcrypto_180.1.1el9.x86_64pigsty1.7 MiBpgsmcrypto_18-0.1.1-4PGSTY.el9.x86_64.rpm
pgsmcrypto_180.1.1el9.aarch64pigsty1.5 MiBpgsmcrypto_18-0.1.1-4PGSTY.el9.aarch64.rpm
pgsmcrypto_180.1.1el10.x86_64pigsty1.7 MiBpgsmcrypto_18-0.1.1-4PGSTY.el10.x86_64.rpm
pgsmcrypto_180.1.1el10.aarch64pigsty1.5 MiBpgsmcrypto_18-0.1.1-4PGSTY.el10.aarch64.rpm
postgresql-18-pgsmcrypto0.1.1d12.x86_64pigsty1.2 MiBpostgresql-18-pgsmcrypto_0.1.1-4PGSTY~bookworm_amd64.deb
postgresql-18-pgsmcrypto0.1.1d12.aarch64pigsty984.4 KiBpostgresql-18-pgsmcrypto_0.1.1-4PGSTY~bookworm_arm64.deb
postgresql-18-pgsmcrypto0.1.1d13.x86_64pigsty1.2 MiBpostgresql-18-pgsmcrypto_0.1.1-4PGSTY~trixie_amd64.deb
postgresql-18-pgsmcrypto0.1.1d13.aarch64pigsty986.2 KiBpostgresql-18-pgsmcrypto_0.1.1-4PGSTY~trixie_arm64.deb
postgresql-18-pgsmcrypto0.1.1u22.x86_64pigsty1.3 MiBpostgresql-18-pgsmcrypto_0.1.1-4PGSTY~jammy_amd64.deb
postgresql-18-pgsmcrypto0.1.1u22.aarch64pigsty1.1 MiBpostgresql-18-pgsmcrypto_0.1.1-4PGSTY~jammy_arm64.deb
postgresql-18-pgsmcrypto0.1.1u24.x86_64pigsty1.3 MiBpostgresql-18-pgsmcrypto_0.1.1-4PGSTY~noble_amd64.deb
postgresql-18-pgsmcrypto0.1.1u24.aarch64pigsty1.1 MiBpostgresql-18-pgsmcrypto_0.1.1-4PGSTY~noble_arm64.deb
postgresql-18-pgsmcrypto0.1.1u26.x86_64pigsty1.3 MiBpostgresql-18-pgsmcrypto_0.1.1-4PGSTY~resolute_amd64.deb
postgresql-18-pgsmcrypto0.1.1u26.aarch64pigsty1.1 MiBpostgresql-18-pgsmcrypto_0.1.1-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pgsmcrypto_170.1.1el8.x86_64pigsty1.5 MiBpgsmcrypto_17-0.1.1-3PIGSTY.el8.x86_64.rpm
pgsmcrypto_170.1.1el8.aarch64pigsty1.4 MiBpgsmcrypto_17-0.1.1-3PIGSTY.el8.aarch64.rpm
pgsmcrypto_170.1.1el9.x86_64pigsty1.7 MiBpgsmcrypto_17-0.1.1-4PGSTY.el9.x86_64.rpm
pgsmcrypto_170.1.1el9.aarch64pigsty1.5 MiBpgsmcrypto_17-0.1.1-4PGSTY.el9.aarch64.rpm
pgsmcrypto_170.1.1el10.x86_64pigsty1.7 MiBpgsmcrypto_17-0.1.1-4PGSTY.el10.x86_64.rpm
pgsmcrypto_170.1.1el10.aarch64pigsty1.5 MiBpgsmcrypto_17-0.1.1-4PGSTY.el10.aarch64.rpm
postgresql-17-pgsmcrypto0.1.1d12.x86_64pigsty1.2 MiBpostgresql-17-pgsmcrypto_0.1.1-4PGSTY~bookworm_amd64.deb
postgresql-17-pgsmcrypto0.1.1d12.aarch64pigsty982.3 KiBpostgresql-17-pgsmcrypto_0.1.1-4PGSTY~bookworm_arm64.deb
postgresql-17-pgsmcrypto0.1.1d13.x86_64pigsty1.2 MiBpostgresql-17-pgsmcrypto_0.1.1-4PGSTY~trixie_amd64.deb
postgresql-17-pgsmcrypto0.1.1d13.aarch64pigsty984.2 KiBpostgresql-17-pgsmcrypto_0.1.1-4PGSTY~trixie_arm64.deb
postgresql-17-pgsmcrypto0.1.1u22.x86_64pigsty1.3 MiBpostgresql-17-pgsmcrypto_0.1.1-4PGSTY~jammy_amd64.deb
postgresql-17-pgsmcrypto0.1.1u22.aarch64pigsty1.1 MiBpostgresql-17-pgsmcrypto_0.1.1-4PGSTY~jammy_arm64.deb
postgresql-17-pgsmcrypto0.1.1u24.x86_64pigsty1.3 MiBpostgresql-17-pgsmcrypto_0.1.1-4PGSTY~noble_amd64.deb
postgresql-17-pgsmcrypto0.1.1u24.aarch64pigsty1.1 MiBpostgresql-17-pgsmcrypto_0.1.1-4PGSTY~noble_arm64.deb
postgresql-17-pgsmcrypto0.1.1u26.x86_64pigsty1.3 MiBpostgresql-17-pgsmcrypto_0.1.1-4PGSTY~resolute_amd64.deb
postgresql-17-pgsmcrypto0.1.1u26.aarch64pigsty1.1 MiBpostgresql-17-pgsmcrypto_0.1.1-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pgsmcrypto_160.1.1el8.x86_64pigsty1.5 MiBpgsmcrypto_16-0.1.1-3PIGSTY.el8.x86_64.rpm
pgsmcrypto_160.1.1el8.aarch64pigsty1.4 MiBpgsmcrypto_16-0.1.1-3PIGSTY.el8.aarch64.rpm
pgsmcrypto_160.1.1el9.x86_64pigsty1.7 MiBpgsmcrypto_16-0.1.1-4PGSTY.el9.x86_64.rpm
pgsmcrypto_160.1.1el9.aarch64pigsty1.5 MiBpgsmcrypto_16-0.1.1-4PGSTY.el9.aarch64.rpm
pgsmcrypto_160.1.1el10.x86_64pigsty1.7 MiBpgsmcrypto_16-0.1.1-4PGSTY.el10.x86_64.rpm
pgsmcrypto_160.1.1el10.aarch64pigsty1.5 MiBpgsmcrypto_16-0.1.1-4PGSTY.el10.aarch64.rpm
postgresql-16-pgsmcrypto0.1.1d12.x86_64pigsty1.2 MiBpostgresql-16-pgsmcrypto_0.1.1-4PGSTY~bookworm_amd64.deb
postgresql-16-pgsmcrypto0.1.1d12.aarch64pigsty983.7 KiBpostgresql-16-pgsmcrypto_0.1.1-4PGSTY~bookworm_arm64.deb
postgresql-16-pgsmcrypto0.1.1d13.x86_64pigsty1.2 MiBpostgresql-16-pgsmcrypto_0.1.1-4PGSTY~trixie_amd64.deb
postgresql-16-pgsmcrypto0.1.1d13.aarch64pigsty982.4 KiBpostgresql-16-pgsmcrypto_0.1.1-4PGSTY~trixie_arm64.deb
postgresql-16-pgsmcrypto0.1.1u22.x86_64pigsty1.3 MiBpostgresql-16-pgsmcrypto_0.1.1-4PGSTY~jammy_amd64.deb
postgresql-16-pgsmcrypto0.1.1u22.aarch64pigsty1.1 MiBpostgresql-16-pgsmcrypto_0.1.1-4PGSTY~jammy_arm64.deb
postgresql-16-pgsmcrypto0.1.1u24.x86_64pigsty1.3 MiBpostgresql-16-pgsmcrypto_0.1.1-4PGSTY~noble_amd64.deb
postgresql-16-pgsmcrypto0.1.1u24.aarch64pigsty1.1 MiBpostgresql-16-pgsmcrypto_0.1.1-4PGSTY~noble_arm64.deb
postgresql-16-pgsmcrypto0.1.1u26.x86_64pigsty1.3 MiBpostgresql-16-pgsmcrypto_0.1.1-4PGSTY~resolute_amd64.deb
postgresql-16-pgsmcrypto0.1.1u26.aarch64pigsty1.1 MiBpostgresql-16-pgsmcrypto_0.1.1-4PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pgsmcrypto_150.1.1el8.x86_64pigsty1.4 MiBpgsmcrypto_15-0.1.1-3PIGSTY.el8.x86_64.rpm
pgsmcrypto_150.1.1el8.aarch64pigsty1.4 MiBpgsmcrypto_15-0.1.1-3PIGSTY.el8.aarch64.rpm
pgsmcrypto_150.1.1el9.x86_64pigsty1.7 MiBpgsmcrypto_15-0.1.1-4PGSTY.el9.x86_64.rpm
pgsmcrypto_150.1.1el9.aarch64pigsty1.5 MiBpgsmcrypto_15-0.1.1-4PGSTY.el9.aarch64.rpm
pgsmcrypto_150.1.1el10.x86_64pigsty1.7 MiBpgsmcrypto_15-0.1.1-4PGSTY.el10.x86_64.rpm
pgsmcrypto_150.1.1el10.aarch64pigsty1.5 MiBpgsmcrypto_15-0.1.1-4PGSTY.el10.aarch64.rpm
postgresql-15-pgsmcrypto0.1.1d12.x86_64pigsty1.2 MiBpostgresql-15-pgsmcrypto_0.1.1-4PGSTY~bookworm_amd64.deb
postgresql-15-pgsmcrypto0.1.1d12.aarch64pigsty979.3 KiBpostgresql-15-pgsmcrypto_0.1.1-4PGSTY~bookworm_arm64.deb
postgresql-15-pgsmcrypto0.1.1d13.x86_64pigsty1.2 MiBpostgresql-15-pgsmcrypto_0.1.1-4PGSTY~trixie_amd64.deb
postgresql-15-pgsmcrypto0.1.1d13.aarch64pigsty979.7 KiBpostgresql-15-pgsmcrypto_0.1.1-4PGSTY~trixie_arm64.deb
postgresql-15-pgsmcrypto0.1.1u22.x86_64pigsty1.3 MiBpostgresql-15-pgsmcrypto_0.1.1-4PGSTY~jammy_amd64.deb
postgresql-15-pgsmcrypto0.1.1u22.aarch64pigsty1.1 MiBpostgresql-15-pgsmcrypto_0.1.1-4PGSTY~jammy_arm64.deb
postgresql-15-pgsmcrypto0.1.1u24.x86_64pigsty1.3 MiBpostgresql-15-pgsmcrypto_0.1.1-4PGSTY~noble_amd64.deb
postgresql-15-pgsmcrypto0.1.1u24.aarch64pigsty1.1 MiBpostgresql-15-pgsmcrypto_0.1.1-4PGSTY~noble_arm64.deb
postgresql-15-pgsmcrypto0.1.1u26.x86_64pigsty1.3 MiBpostgresql-15-pgsmcrypto_0.1.1-4PGSTY~resolute_amd64.deb
postgresql-15-pgsmcrypto0.1.1u26.aarch64pigsty1.1 MiBpostgresql-15-pgsmcrypto_0.1.1-4PGSTY~resolute_arm64.deb

源码

pig build pkg pgsmcrypto;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

pig repo add pgsql -u   # add both repo and update cache

使用 pig 安装扩展。

pig install pgsmcrypto;		# install by package name

pig install pgsmcrypto -v 19;   # install for PG 19
pig install pgsmcrypto -v 18;   # install for PG 18
pig install pgsmcrypto -v 17;   # install for PG 17
pig install pgsmcrypto -v 16;   # install for PG 16
pig install pgsmcrypto -v 15;   # install for PG 15

在数据库中创建扩展。

CREATE EXTENSION pgsmcrypto;

用法

pgsmcrypto: PostgreSQL 国密算法扩展

pgsmcrypto 为 PostgreSQL 提供中国国密(SM 系列)算法,包括 SM3 哈希、SM2 非对称加密/签名和 SM4 对称加密。

CREATE EXTENSION pgsmcrypto;

SM3 消息摘要

SELECT sm3_hash_string('abc');              -- 返回64字符十六进制字符串(32字节)
SELECT sm3_hash('abc'::bytea);              -- 对 bytea 输入求哈希
SELECT sm3_hash(E'\\x616263');              -- 对原始十六进制输入求哈希

SM2 非对称加密

密钥生成

SELECT sm2_gen_keypair();                   -- 返回 {私钥, 公钥} 数组
SELECT sm2_privkey_valid('f774...');        -- 验证私钥(1=有效)
SELECT sm2_pubkey_valid('8093...');         -- 验证公钥(1=有效)
SELECT sm2_pk_from_sk('f774...');           -- 从私钥派生公钥

密钥导出/导入(PEM)

SELECT sm2_keypair_to_pem_bytes('f774...');       -- 私钥转 PEM
SELECT sm2_pubkey_to_pem_bytes('8093...');        -- 公钥转 PEM
SELECT sm2_keypair_from_pem_bytes(pem_bytes);     -- 从 PEM 导入
SELECT sm2_pubkey_from_pem_bytes(pem_bytes);      -- 从 PEM 导入公钥

签名与验签

-- 裸签名/验签(直接签名消息)
WITH s AS (
    SELECT sm2_sign_raw('abc'::bytea, 'f774...') AS sig
)
SELECT sm2_verify_raw('abc'::bytea, sig, '8093...') FROM s;

-- 标准签名/验签(SM2规范,带 id + SM3 摘要)
WITH s AS (
    SELECT sm2_sign('myid'::bytea, 'abc'::bytea, 'f774...') AS sig
)
SELECT sm2_verify('myid'::bytea, 'abc'::bytea, sig, '8093...') FROM s;

加密与解密

-- 标准加密/解密
WITH c AS (
    SELECT sm2_encrypt('abc'::bytea, '8093...') AS enc
)
SELECT sm2_decrypt(enc, 'f774...') FROM c;

-- 还提供:sm2_encrypt_c1c2c3、sm2_encrypt_asna1、sm2_encrypt_hex、sm2_encrypt_base64
-- 以及对应的解密变体

SM4 对称加密

-- ECB 模式(密钥必须为16字节)
SELECT sm4_encrypt_ecb('abc'::bytea, '1234567812345678'::bytea);
SELECT sm4_decrypt_ecb(encrypted, '1234567812345678'::bytea);

-- CBC 模式(密钥和 IV 必须为16字节)
SELECT sm4_encrypt_cbc('abc'::bytea, '1234567812345678'::bytea, '0000000000000000'::bytea);
SELECT sm4_decrypt_cbc(encrypted, '1234567812345678'::bytea, '0000000000000000'::bytea);

表格阅读器