存储加密

pgcrypto 提供了两类数据加密算法:单向加密和双向加密,用以实现数据库层面的数据加密存储,保障数据的安全使用。

  • 单向加密属于不可逆加密,无法根据密文解密出明文。适用于数据验证,例如登录密码验证。常用的单向加密算法包括 MD5、SHA、HMAC 等。

  • 双向加密属于可逆加密,根据密文和密钥可解密出明文。适用于数据的安全传输,例如电子支付、数字签名等。常用的双向加密算法包括 AES、DES、RSA、ECC 、SM4等。

单向加密

普通哈希函数

  • digest() 函数可以根据不同的算法生成数据的二进制哈希值。这类加密算法对相同的数据经过加密之后的结果相同。

    #语法结构
    digest(data text, type text) returns bytea
    digest(data bytea, type text) returns bytea
    #type 是要使用的算法,标准算法包括 md5、sha1、sha224、sha256、sha384 和 sha512。
    
    #使用示例
    
    INSERT INTO users(username, password) 
    VALUES ('tony', encode(digest('123456','md5'), 'hex'));
    
    INSERT INTO users(username, password) 
    VALUES ('anne', encode(digest('123456','md5'), 'hex'));
    
    SELECT * FROM users;
    id|username|password                        |
    --|--------|--------------------------------|
     1|tony    |e10adc3949ba59abbe56e057f20f883e|
     2|anne    |e10adc3949ba59abbe56e057f20f883e|
    
    #匹配时需要使用encode函数进行十六禁止转换
    SELECT id FROM users
    WHERE username = 'tony' AND password = encode(digest('123456','md5'), 'hex');
    id|
    --|
     1|
  • hmac() 函数可以为带有密钥 pws 的数据根据不同的算法生成数据的二进制哈希值。这与digest()相似,但该哈希只能在密钥相同的情况下才能被重新计算出相同的加密结果。

    #语法结构
    hmac(data text, psw text, type text) returns bytea
    hmac(data bytea, psw text, type text) returns bytea
    #type 是要使用的算法,标准算法包括 md5、sha1、sha224、sha256、sha384 和 sha512。

    双向加密

PGP 加密函数

PGP 加密函数实现了 OpenPGP(RFC4880)标准中的加密功能,包括对称密钥加密(私钥加密)和非对称密钥加密(公钥加密)。

  #加密
  pgp_sym_encrypt(data text, psw text [, options text ]) returns bytea
  pgp_sym_encrypt_bytea(data bytea, psw text [, options text ]) returns bytea
  #解密
  pgp_sym_decrypt(msg bytea, psw text [, options text ]) returns text
  pgp_sym_decrypt_bytea(msg bytea, psw text [, options text ]) returns bytea


  #应用示例

  select pgp_sym_encrypt('abc','key_value');
                                                                 pgp_sym_encrypt                                                               
  ----------------------------------------------------------------------------------------------------------------------------------------------
   \xc30d04070302f93fbd59b40bf7fd71d2340175c19d234d275f5b8ae668fecbbdfd80f0e94185f07dee15cb6d2b0dfbfdf08c98648e07da8f3d8902bb3dd349fdb36860a1ff
  (1 row)

  select pgp_sym_decrypt(pgp_sym_encrypt('abc','key_value'),'key_value');             
   pgp_sym_decrypt
  -----------------
   abc
  (1 row)

  # 常用的option有:
  # cipher-algo,使用的密码算法,可以是 bf、aes128(默认值)、aes192、aes256;
  # compress-algo,使用的压缩算法,只有编译 PostgreSQL 时使用了 zlib 参数可用。可以是:0,不压缩,默认值;1,ZIP 压缩;2,ZLIB 压缩(ZIP 加上元数据和 CRC)

SM4 加密函数

SM4 是一种对称加密算法,属于双向加密,它是中国国家密码管理局发布的商用密码算法标准。采用 128 位分组和密钥,通过多轮迭代实现高效的数据加密和解密,广泛应用于移动通信、物联网、金融等领域。

  #加密
  encrypt(data text, psw text [, options text ]) returns bytea;
  #解密
  decrypt(msg bytea, psw text [, options text ]) returns bytea;

  #应用示例
  create table t1 (c1 bytea);

  insert into t1 select encrypt('123456', '1462846455', 'sm4');

   select * from t1;
                   c1                 
  ------------------------------------
   \xaf8aac27b4721d733852c18e9a6dd126
  (1 row)

  select convert_from(public.decrypt(c1, '1462846455', 'sm4'),'utf-8') from t1;
   convert_from 
  --------------
   123456
  (1 row)

  # 常用的option有:
  # cipher-algo,使用的密码算法是 sm4