pgsmcrypto
pgsmcrypto
pgsmcrypto : PostgreSQL SM Algorithm Extension
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 7060 | pgsmcrypto
|
pgsmcrypto
|
0.1.1 |
SEC
|
MIT
|
Rust
|
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d--
|
No
|
Yes
|
No
|
Yes
|
no
|
no
|
| Relationships | |
|---|---|
| See Also | pgsodium
pgcryptokey
pgcrypto
pg_tde
sslutils
faker
uuid-ossp
lo
|
manual updated pgrx by Vonng
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY
|
0.1.1 |
18
17
16
15
14
|
pgsmcrypto |
- |
| RPM | PIGSTY
|
0.1.1 |
18
17
16
15
14
|
pgsmcrypto_$v |
- |
| DEB | PIGSTY
|
0.1.1 |
18
17
16
15
14
|
postgresql-$v-pgsmcrypto |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
el8.aarch64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
el9.x86_64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
el9.aarch64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
el10.x86_64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
el10.aarch64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
d12.x86_64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
d12.aarch64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
d13.x86_64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
d13.aarch64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
u22.x86_64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
u22.aarch64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
u24.x86_64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
u24.aarch64
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
PIGSTY 0.1.1
|
Source
pig build pkg pgsmcrypto; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pgsmcrypto; # install via package name, for the active PG version
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
pig install pgsmcrypto -v 14; # install for PG 14Create 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 inputSM2 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 keyKey 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 PEMSign 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 variantsSM4 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);Last updated on