Loading
CLOUD09_SPACE
0%
INITIALIZING
DOC_ID // 3758d8ONLINE

oushuDB之pgcrypto加密解密

UPDATED: 2026-6-4
3085 CHARS
CLOUD09

参考

pgcrypto安装

功能介绍

digest()

根据给定的算法获取给定数据的hash值。
标准算法支持有md5、sha1、sha224、sha256、sha384和sha512
e.g.

hmac()

用key计算hash值,type和digest一样,hmac和digest类似,但是只有知道key的情况下才能计算出哈希值, 这样可以预防更改数据以及更改哈希匹配的情况,如果key大于hash block size,那么先计算哈希值,哈希值作为key使用
e.g.

密码哈希函数

crypt()用来计算hash值.
gen_salt()随机产生一个值作为crypt()的算法参数.
gen_salt()的type参数为des, xdes, md5, bf.
gen_salt()的iter_count指迭代次数, 数字越大加密时间越长, 被破解需要的时间也越长.
crypt()和gen_salt()的组合主要是提高了逆向破解的难度, 增强了数据的安全性

crypt()支持的算法

算法
最大密码长度
适应?
Salt bits
描述
bf
72
Yes
128
Blowfish-based, variant 2a
md5
unlimited
No
48
MD5-based crypt
xdes
8
Yes
24
Extended DES
des
8
No
12
Original UNIX crypt

crypt()迭代次数

算法
默认
最小值
最大值
xdes
725
1
16777215
bf
8
4
31
  • xdes额外限制,只能是奇数

hash算法速度

算法
Hashes/sec
For [a-z]
For [A-Za-z0-9]
crypt-bf/8
28
246 years
251322 years
crypt-bf/7
57
121 years
123457 years
crypt-bf/6
112
62 years
62831 years
crypt-bf/5
211
33 years
33351 years
crypt-md5
2681
2.6 years
2625 years
crypt-des
362837
7 days
19 years
sha1
590223
4 days
12 years
md5
2345086
1 day
3 years
crypt和gen_salt是以牺牲hash速度为代价来换取安全性的
e.g.

PGP 加密函数

该功能实现了部分OpenPGP (RFC 4880)标准的加密。支持对称秘钥公共秘钥的加密。
一条加密的PGP消息包含2个部分,或数据包:
  • 数据包包含一个会话秘钥—加密了的对称秘钥或者是公共秘钥。
  • 数据包包含带有会话秘钥的加密数据。

公共秘钥

pgp_key_id()

pgp_key_id抽取一个 PGP 公钥或私钥的密钥 ID。或者如果给定了一个加密过的消息,它给出一个用来加密数据的密钥 ID。
它能够返回 2 个特殊密钥 ID:
  • SYMKEY
    • 该消息是用一个对称密钥加密的。
  • ANYKEY
    • 该消息是用公钥加密的,但是密钥 ID 已经被移除。这意味着你将需要尝试你所有的密钥来看看哪个能解密该消息。pgcrypto本身不产生这样的消息。
注意不同的密钥可能具有相同的 ID。这很少见但是是一种正常事件。客户端应用则应该尝试用每一个去解密,看看哪个合适 — 像处理ANYKEY一样

armor(), dearmor()

这些函数把二进制数据包装/解包成 PGP ASCII-armored 格式,其基本上是带有 CRC 和额外格式化的 Base64。

pgp_pub_encrypt()

用一个公共 PGP 密钥 key加密data。给这个函数一个私钥会产生一个错误。
options参数可以包含下文所述的选项设置

pgp_pub_decrypt()

解密一个公共密钥加密的消息。key必须是对应于用来加密的公钥的私钥。如果私钥是用口令保护的,你必须在psw中给出该口令。如果没有口令,但你想要指定选项,你需要给出一个空口令。
不允许使用pgp_pub_decrypt解密bytea数据。这是为了避免输出非法的字符数据。使用pgp_pub_decrypt_bytea解密原始文本数据是好的。
options参数可以包含下文所述的选项设置。

使用举例

对称秘钥

pgp_sym_encrypt()

带有一个对称的PGP秘钥psw加密dataoptions参数可以包含选项设置。
e.g.

pgp_sym_decrypt()

解密一个对称秘钥加密的PGP消息。
pgp_sym_decrypt解密bytea数据是不允许的。
这是为了避免输出不合法的字符数据。
pgp_sym_decrypt_bytea 解密原始的文本数据是可以的。
options参数可以包含选项设置。
e.g.

PGP功能的选项

compress-algo

只有PostgreSQL编译的时候带有zlib选项时才可以使用下来该选项的压缩算法
e.g.

unicode-mode

Whether to convert textual data from database internal encoding to UTF-8 and back. If your database already is UTF-8, no conversion will be done, but the message will be tagged as UTF-8. Without this option it will not be.
e.g.

compress-level

How much to compress. Higher levels compress smaller but are slower. 0 disables compression.
e.g.

cipher-algo

Which cipher algorithm to use.
e.g.

convert-crlf

Whether to convert \n into \r\n when encrypting and \r\n to \n when decrypting. RFC 4880 specifies that text data should be stored using \r\n line-feeds. Use this to get fully RFC-compliant behavior.
e.g.

disable-mdc

Do not protect data with SHA-1. The only good reason to use this option is to achieve compatibility with ancient PGP products, predating the addition of SHA-1 protected packets to RFC 4880. Recent gnupg.org and pgp.com software supports it fine.
e.g.

s2k-mode

Which S2K algorithm to use.
e.g.

s2k-digest-algo

Which digest algorithm to use in S2K calculation.
e.g.

s2k-cipher-algo

Which cipher to use for encrypting separate session key.
e.g.

enable-session-key

Use separate session key. Public-key encryption always uses a separate session key; this is for symmetric-key encryption, which by default uses the S2K key directly.
e.g.

PGP功能的选项的复合选项

 
 
NAVIGATION // Related Articles
Loading...
© 2021-2026 Tangly