Skip to content

pgsmcrypto

PostgreSQL SM Algorithm Extension

pgsmcrypto · PostgreSQL SM Algorithm Extension

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
7080pgsmcryptopgsmcrypto0.1.1SECMITRust
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--NoYesNoYesnono

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
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
PackageVersionOSORGSIZEFile URL
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
PackageVersionOSORGSIZEFile URL
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
PackageVersionOSORGSIZEFile URL
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
PackageVersionOSORGSIZEFile URL
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
PackageVersionOSORGSIZEFile URL
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

Source

pig build pkg pgsmcrypto;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with 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 this extension with:

CREATE EXTENSION pgsmcrypto;

Usage

pgsmcrypto: SM national cryptographic algorithm extension for PostgreSQL

pgsmcrypto provides Chinese national cryptographic (SM series) algorithms for PostgreSQL, including SM3 hashing, SM2 asymmetric encryption/signing, and SM4 symmetric encryption.

CREATE EXTENSION pgsmcrypto;

SM3 Message Digest

SELECT sm3_hash_string('abc');              -- Returns 64-char hex string (32 bytes)
SELECT sm3_hash('abc'::bytea);              -- Hash bytea input
SELECT sm3_hash(E'\\x616263');              -- Hash raw hex input

SM2 Asymmetric Encryption

Key Generation

SELECT sm2_gen_keypair();                   -- Returns {private_key, public_key} array
SELECT sm2_privkey_valid('f774...');        -- Validate private key (1=valid)
SELECT sm2_pubkey_valid('8093...');         -- Validate public key (1=valid)
SELECT sm2_pk_from_sk('f774...');           -- Derive public key from private key

Key Export/Import (PEM)

SELECT sm2_keypair_to_pem_bytes('f774...');       -- Private key to PEM
SELECT sm2_pubkey_to_pem_bytes('8093...');        -- Public key to PEM
SELECT sm2_keypair_from_pem_bytes(pem_bytes);     -- Import from PEM
SELECT sm2_pubkey_from_pem_bytes(pem_bytes);      -- Import public key from PEM

Sign and Verify

-- Raw sign/verify (signs message directly)
WITH s AS (
    SELECT sm2_sign_raw('abc'::bytea, 'f774...') AS sig
)
SELECT sm2_verify_raw('abc'::bytea, sig, '8093...') FROM s;

-- Standard sign/verify (SM2 specification with id + SM3 digest)
WITH s AS (
    SELECT sm2_sign('myid'::bytea, 'abc'::bytea, 'f774...') AS sig
)
SELECT sm2_verify('myid'::bytea, 'abc'::bytea, sig, '8093...') FROM s;

Encrypt and Decrypt

-- Standard encrypt/decrypt
WITH c AS (
    SELECT sm2_encrypt('abc'::bytea, '8093...') AS enc
)
SELECT sm2_decrypt(enc, 'f774...') FROM c;

-- Also available: sm2_encrypt_c1c2c3, sm2_encrypt_asna1, sm2_encrypt_hex, sm2_encrypt_base64
-- with corresponding decrypt variants

SM4 Symmetric Encryption

-- ECB mode (key must be 16 bytes)
SELECT sm4_encrypt_ecb('abc'::bytea, '1234567812345678'::bytea);
SELECT sm4_decrypt_ecb(encrypted, '1234567812345678'::bytea);

-- CBC mode (key and IV must be 16 bytes)
SELECT sm4_encrypt_cbc('abc'::bytea, '1234567812345678'::bytea, '0000000000000000'::bytea);
SELECT sm4_decrypt_cbc(encrypted, '1234567812345678'::bytea, '0000000000000000'::bytea);

Table reader