postgresql 加解密

F.20. pgcrypto

Thepgcryptomodule provides cryptographic functions forPostgreSQL.

F.20.1. General hashing functions

F.20.1.1.digest()

    digest(data text,type text) returns bytea
    digest(data bytea,type text) returns bytea
   

Computes a binary hash of the givendata.typeis the algorithm to use. Standard algorithms aremd5,sha1,sha224,sha256,sha384andsha512. Ifpgcryptowas built with OpenSSL,more algorithms are available,as detailed inTable F-21.

If you want the digest as a hexadecimal string,useencode()on the result. For example:

    CREATE OR REPLACE FUNCTION sha1(bytea) returns text AS $$
      SELECT encode(digest($1,'sha1'),'hex')
    $$ LANGUAGE SQL STRICT IMMUTABLE;
   

F.20.1.2.hmac()

    hmac(data text,key text,type text) returns bytea
    hmac(data bytea,type text) returns bytea
   

Calculates hashed MAC fordatawith keykey.typeis the same as indigest().

This is similar todigest()but the hash can only be recalculated knowing the key. This prevents the scenario of someone altering data and also changing the hash to match.

If the key is larger than the hash block size it will first be hashed and the result will be used as key.

F.20.2. Password hashing functions

The functionscrypt()andgen_salt()are specifically designed for hashing passwords.crypt()does the hashing andgen_salt()prepares algorithm parameters for it.

The algorithms incrypt()differ from usual hashing algorithms like MD5 or SHA1 in the following respects:

  1. They are slow. As the amount of data is so small,this is the only way to make brute-forcing passwords hard.

  2. They use a random value,called thesalt,so that users having the same password will have different encrypted passwords. This is also an additional defense against reversing the algorithm.

  3. They include the algorithm type in the result,so passwords hashed with different algorithms can co-exist.

  4. Some of them are adaptive — that means when computers get faster,you can tune the algorithm to be slower,without introducing incompatibility with existing passwords.

Table F-18. Supported algorithms forcrypt()

Algorithm Max password length Adaptive? Salt bits Description
bf 72 yes 128 Blowfish-based,variant 2a
md5 unlimited no 48 MD5-based crypt
xdes 8 24 Extended DES
des 12 Original UNIX crypt

F.20.2.1.crypt()

    crypt(password text,salt text) returns text
   

Calculates a crypt(3)-style hash ofpassword. When storing a new password,you need to usegen_salt()to generate a newsaltvalue. To check a password,pass the stored hash value assalt,and test whether the result matches the stored value.

Example of setting a new password:

    UPDATE ... SET pswhash = crypt('new password',gen_salt('md5'));
   

Example of authentication:

    SELECT pswhash = crypt('entered password',pswhash) FROM ... ;
   

This returnstrueif the entered password is correct.

F.20.2.2.gen_salt()

    gen_salt(type text [,iter_count integer ]) returns text
   

Generates a new random salt string for use incrypt(). The salt string also tellscrypt()which algorithm to use.

Thetypeparameter specifies the hashing algorithm. The accepted types are:des,xdes,md5andbf.

Theiter_countparameter lets the user specify the iteration count,for algorithms that have one. The higher the count,the more time it takes to hash the password and therefore the more time to break it. Although with too high a count the time to calculate a hash may be several years — which is somewhat impractical. If theiter_countparameter is omitted,the default iteration count is used. Allowed values foriter_countdepend on the algorithm:

Table F-19. Iteration counts forcrypt()

Algorithm Default Min Max
xdes 725 1 16777215
bf 6 4 31

Forxdesthere is an additional limitation that the iteration count must be an odd number.

To pick an appropriate iteration count,consider that the original DES crypt was designed to have the speed of 4 hashes per second on the hardware of that time. Slower than 4 hashes per second would probably dampen usability. Faster than 100 hashes per second is probably too fast.

Here is a table that gives an overview of the relative slowness of different hashing algorithms. The table shows how much time it would take to try all combinations of characters in an 8-character password,assuming that the password contains either only lowercase letters,or upper- and lower-case letters and numbers. In thecrypt-bfentries,the number after a slash is theiter_countparameter ofgen_salt.

Table F-20. Hash algorithm speeds

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

Notes:

  • The machine used is a 1.5GHz Pentium 4.

  • crypt-desandcrypt-md5algorithm numbers are taken from John the Ripper v1.6.38-testoutput.

  • md5numbers are from mdcrack 1.2.

  • sha1numbers are from lcrack-20031130-beta.

  • crypt-bfnumbers are taken using a simple program that loops over 1000 8-character passwords. That way I can show the speed with different numbers of iterations. For reference:john -testshows 213 loops/sec forcrypt-bf/5. (The very small difference in results is in accordance with the fact that thecrypt-bfimplementation inpgcryptois the same one used in John the Ripper.)

Note that"try all combinations"is not a realistic exercise. Usually password cracking is done with the help of dictionaries,which contain both regular words and various mutations of them. So,even somewhat word-like passwords could be cracked much faster than the above numbers suggest,while a 6-character non-word-like password may escape cracking. Or not.

F.20.3. PGP encryption functions

The functions here implement the encryption part of the OpenPGP (RFC 4880) standard. Supported are both symmetric-key and public-key encryption.

An encrypted PGP message consists of 2 parts,orpackets:

  • Packet containing a session key — either symmetric-key or public-key encrypted.

  • Packet containing data encrypted with the session key.

When encrypting with a symmetric key (i.e.,a password):

  1. The given password is hashed using a String2Key (S2K) algorithm. This is rather similar tocrypt()algorithms — purposefully slow and with random salt — but it produces a full-length binary key.

  2. If a separate session key is requested,a new random key will be generated. Otherwise the S2K key will be used directly as the session key.

  3. If the S2K key is to be used directly,then only S2K settings will be put into the session key packet. Otherwise the session key will be encrypted with the S2K key and put into the session key packet.

When encrypting with a public key:

  1. A new random session key is generated.

  2. It is encrypted using the public key and put into the session key packet.

In either case the data to be encrypted is processed as follows:

  1. Optional data-manipulation: compression,conversion to UTF-8,and/or conversion of line-endings.

  2. The data is prefixed with a block of random bytes. This is equivalent to using a random IV.

  3. An SHA1 hash of the random prefix and data is appended.

  4. All this is encrypted with the session key and placed in the data packet.

F.20.3.1.pgp_sym_encrypt()

    pgp_sym_encrypt(data text,psw text [,options text ]) returns bytea
    pgp_sym_encrypt_bytea(data bytea,options text ]) returns bytea
   

Encryptdatawith a symmetric PGP keypsw. Theoptionsparameter can contain option settings,as described below.

F.20.3.2.pgp_sym_decrypt()

    pgp_sym_decrypt(msg bytea,options text ]) returns text
    pgp_sym_decrypt_bytea(msg bytea,options text ]) returns bytea
   

Decrypt a symmetric-key-encrypted PGP message.

Decrypting bytea data withpgp_sym_decryptis disallowed. This is to avoid outputting invalid character data. Decrypting originally textual data withpgp_sym_decrypt_byteais fine.

Theoptionsparameter can contain option settings,102)"> F.20.3.3.pgp_pub_encrypt()

    pgp_pub_encrypt(data text,key bytea [,options text ]) returns bytea
    pgp_pub_encrypt_bytea(data bytea,options text ]) returns bytea
   

Encryptdatawith a public PGP keykey. Giving this function a secret key will produce a error.

Theoptionsparameter can contain option settings,102)"> F.20.3.4.pgp_pub_decrypt()

    pgp_pub_decrypt(msg bytea,options text ]]) returns text
    pgp_pub_decrypt_bytea(msg bytea,options text ]]) returns bytea
   

Decrypt a public-key-encrypted message.keymust be the secret key corresponding to the public key that was used to encrypt. If the secret key is password-protected,you must give the password inpsw. If there is no password,but you want to specify options,you need to give an empty password.

Decrypting bytea data withpgp_pub_decryptis disallowed. This is to avoid outputting invalid character data. Decrypting originally textual data withpgp_pub_decrypt_byteais fine.

Theoptionsparameter can contain option settings,102)"> F.20.3.5.pgp_key_id()

    pgp_key_id(bytea) returns text
   

pgp_key_idextracts the key ID of a PGP public or secret key. Or it gives the key ID that was used for encrypting the data,if given an encrypted message.

It can return 2 special key IDs:

  • SYMKEY

    The message is encrypted with a symmetric key.

  • ANYKEY

    The message is public-key encrypted,but the key ID has been removed. That means you will need to try all your secret keys on it to see which one decrypts it.pgcryptoitself does not produce such messages.

Note that different keys may have the same ID. This is rare but a normal event. The client application should then try to decrypt with each one,to see which fits — like handlingANYKEY.

F.20.3.6.armor(),dearmor()

    armor(data bytea) returns text
    dearmor(data text) returns bytea
   

These functions wrap/unwrap binary data into PGP Ascii Armor format,which is basically Base64 with CRC and additional formatting.

F.20.3.7. Options for PGP functions

Options are named to be similar to GnuPG. An option's value should be given after an equal sign; separate options from each other with commas. For example:

    pgp_sym_encrypt(data,psw,'compress-algo=1,cipher-algo=aes256')
   

All of the options exceptconvert-crlfapply only to encrypt functions. Decrypt functions get the parameters from the PGP data.

The most interesting options are probablycompress-algoandunicode-mode. The rest should have reasonable defaults.

F.20.3.7.1. cipher-algo

Which cipher algorithm to use.

    Values: bf,aes128,aes192,aes256 (OpenSSL-only: 3des,cast5)
    Default: aes128
    Applies to: pgp_sym_encrypt,pgp_pub_encrypt
   

F.20.3.7.2. compress-algo

Which compression algorithm to use. Only available ifPostgreSQLwas built with zlib.

    Values:
      0 - no compression
      1 - ZIP compression
      2 - ZLIB compression (= ZIP plus meta-data and block CRCs)
    Default: 0
    Applies to: pgp_sym_encrypt,102)"> F.20.3.7.3. compress-level 
   

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

    Values: 0,1-9
    Default: 6
    Applies to: pgp_sym_encrypt,102)"> F.20.3.7.4. convert-crlf 
   

Whether to convert\ninto\r\nwhen encrypting and\r\nto\nwhen decrypting. RFC 4880 specifies that text data should be stored using\r\nline-feeds. Use this to get fully RFC-compliant behavior.

    Values: 0,1
    Default: 0
    Applies to: pgp_sym_encrypt,pgp_pub_encrypt,pgp_sym_decrypt,pgp_pub_decrypt
   

F.20.3.7.5. 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.

    Values: 0,102)"> F.20.3.7.6. 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.

    Values: 0,1
    Default: 0
    Applies to: pgp_sym_encrypt
   

F.20.3.7.7. s2k-mode

Which S2K algorithm to use.

    Values:
      0 - Without salt.  Dangerous!
      1 - With salt but with fixed iteration count.
      3 - Variable iteration count.
    Default: 3
    Applies to: pgp_sym_encrypt
   

F.20.3.7.8. s2k-digest-algo

Which digest algorithm to use in S2K calculation.

    Values: md5,sha1
    Default: sha1
    Applies to: pgp_sym_encrypt
   

F.20.3.7.9. s2k-cipher-algo

Which cipher to use for encrypting separate session key.

    Values: bf,aes,aes256
    Default: use cipher-algo
    Applies to: pgp_sym_encrypt
   

F.20.3.7.10. 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.

    Values: 0,pgp_pub_encrypt
   

F.20.3.8. Generating PGP keys with GnuPG

To generate a new key:

   gpg --gen-key
  

The preferred key type is"DSA and Elgamal".

For RSA encryption you must create either DSA or RSA sign-only key as master and then add an RSA encryption subkey withgpg --edit-key.

To list keys:

   gpg --list-secret-keys
  

To export a public key in ascii-armor format:

   gpg -a --export KEYID > public.key
  

To export a secret key in ascii-armor format:

   gpg -a --export-secret-keys KEYID > secret.key
  

You need to usedearmor()on these keys before giving them to the PGP functions. Or if you can handle binary data,you can drop-afrom the command.

For more details seeman gpg,The GNU Privacy Handbookand other documentation onhttp://www.gnupg.org.

F.20.3.9. Limitations of PGP code

  • No support for signing. That also means that it is not checked whether the encryption subkey belongs to the master key.

  • No support for encryption key as master key. As such practice is generally discouraged,this should not be a problem.

  • No support for several subkeys. This may seem like a problem,as this is common practice. On the other hand,you should not use your regular GPG/PGP keys withpgcrypto,but create new ones,as the usage scenario is rather different.

F.20.4. Raw encryption functions

These functions only run a cipher over data; they don't have any advanced features of PGP encryption. Therefore they have some major problems:

  1. They use user key directly as cipher key.

  2. They don't provide any integrity checking,to see if the encrypted data was modified.

  3. They expect that users manage all encryption parameters themselves,even IV.

  4. They don't handle text.

So,with the introduction of PGP encryption,usage of raw encryption functions is discouraged.

    encrypt(data bytea,key bytea,type text) returns bytea
    decrypt(data bytea,type text) returns bytea

    encrypt_iv(data bytea,iv bytea,type text) returns bytea
    decrypt_iv(data bytea,type text) returns bytea
  

Encrypt/decrypt data using the cipher method specified bytype. The syntax of thetypestring is:

   algorithm [ - mode ] [ /pad: padding ]
  

wherealgorithmis one of:

  • bf— Blowfish

  • aes— AES (Rijndael-128)

andmodeis one of:

  • cbc— next block depends on previous (default)

  • ecb— each block is encrypted separately (for testing only)

andpaddingis one of:

  • pkcs— data may be any length (default)

  • none— data must be multiple of cipher block size

So,for example,these are equivalent:

   encrypt(data,'fooz','bf')
   encrypt(data,'bf-cbc/pad:pkcs')
  

Inencrypt_ivanddecrypt_iv,theivparameter is the initial value for the CBC mode; it is ignored for ECB. It is clipped or padded with zeroes if not exactly block size. It defaults to all zeroes in the functions without this parameter.

F.20.5. Random-data functions
   gen_random_bytes(count integer) returns bytea
  

Returnscountcryptographically strong random bytes. At most 1024 bytes can be extracted at a time. This is to avoid draining the randomness generator pool.

F.20.6. Notes

F.20.6.1. Configuration

pgcryptoconfigures itself according to the findings of the main PostgreSQLconfigurescript. The options that affect it are--with-zliband--with-openssl.

When compiled with zlib,PGP encryption functions are able to compress data before encrypting.

When compiled with OpenSSL,there will be more algorithms available. Also public-key encryption functions will be faster as OpenSSL has more optimized BIGNUM functions.

Table F-21. Summary of functionality with and without OpenSSL

Functionality Built-in With OpenSSL
MD5 yes yes
SHA1 SHA224/256/384/512 yes (Note 1)
Other digest algorithms no yes (Note 2)
Blowfish AES yes (Note 3)
DES/3DES/CAST5 Raw encryption PGP Symmetric encryption PGP Public-Key encryption yes

Notes:

  1. SHA2 algorithms were added to OpenSSL in version 0.9.8. For older versions,pgcryptowill use built-in code.

  2. Any digest algorithm OpenSSL supports is automatically picked up. This is not possible with ciphers,which need to be supported explicitly.

  3. AES is included in OpenSSL since version 0.9.7. For older versions,pgcryptowill use built-in code.

F.20.6.2. NULL handling

As is standard in SQL,all functions return NULL,if any of the arguments are NULL. This may create security risks on careless usage.

F.20.6.3. Security limitations

Allpgcryptofunctions run inside the database server. That means that all the data and passwords move betweenpgcryptoand client applications in clear text. Thus you must:

  1. Connect locally or use SSL connections.

  2. Trust both system and database administrator.

If you cannot,then better do crypto inside client application.

F.20.6.4. Useful reading

F.20.6.5. Technical references

F.20.7. Author

Marko Kreen

pgcryptouses code from the following sources:

Table F-22. Credits

Author Source origin
DES crypt David Burren and others FreeBSD libcrypt
MD5 crypt Poul-Henning Kamp Blowfish crypt Solar Designer www.openwall.com
Blowfish cipher Simon Tatham PuTTY
Rijndael cipher Brian Gladman OpenBSD sys/crypto
MD5 and SHA1 WIDE Project KAME kame/sys/crypto
SHA256/384/512 Aaron D. Gifford BIGNUM math Michael J. Fromberger dartmouth.edu/~sting/sw/imath

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


文章浏览阅读601次。Oracle的数据导入导出是一项基本的技能,但是对于懂数据库却不熟悉Oracle的同学可能会有一定的障碍。正好在最近的一个项目中碰到了这样一个任务,于是研究了一下Oracle的数据导入导出,在这里跟大家分享一下。......_oracle 迁移方法 对比
文章浏览阅读553次。开头还是介绍一下群,如果感兴趣polardb ,mongodb ,mysql ,postgresql ,redis 等有问题,有需求都可以加群群内有各大数据库行业大咖,CTO,可以解决你的问题。加群请联系 liuaustin3 ,在新加的朋友会分到2群(共700多人左右 1 + 2)。最近我们在使用MYSQL 8 的情况下(8.025)在数据库运行中出现一个问题 参数prefer_order_i..._mysql prefer_ordering_index
文章浏览阅读3.5k次,点赞3次,收藏7次。折腾了两个小时多才成功连上,在这分享一下我的经验,也仅仅是经验分享,有不足的地方欢迎大家在评论区补充交流。_navicat连接opengauss
文章浏览阅读2.7k次。JSON 代表 JavaScript Object Notation。它是一种开放标准格式,将数据组织成中详述的键/值对和数组。_postgresql json
文章浏览阅读2.9k次,点赞2次,收藏6次。navicat 连接postgresql 注:navicat老版本可能报错。1.在springboot中引入我们需要的依赖以及相应版本。用代码生成器生成代码后,即可进行增删改查(略)安装好postgresql 略。更改配置信息(注释中有)_mybatisplus postgresql
文章浏览阅读1.4k次。postgre进阶sql,包含分组排序、JSON解析、修改、删除、更新、强制踢出数据库所有使用用户、连表更新与删除、获取今年第一天、获取近12个月的年月、锁表处理、系统表使用(查询所有表和字段及注释、查询表占用空间)、指定数据库查找模式search_path、postgre备份及还原_pgsql分组取每组第一条
文章浏览阅读3.3k次。上一篇我们学习了日志清理,日志清理虽然解决了日志膨胀的问题,但就无法再恢复检查点之前的一致性状态。因此,我们还需要日志归档,pg的日志归档原理和Oracle类似,不过归档命令需要自己配置。以下代码在postmaster.c除了开启归档外,还需要保证wal_level不能是MINIMAL状态(因为该状态下有些操作不会记录日志)。在db启动时,会同时检查archive_mode和wal_level。以下代码也在postmaster.c(PostmasterMain函数)。......_postgresql archive_mode
文章浏览阅读3k次。系统:ubuntu22.04.3目的:利用向日葵实现windows远程控制ubuntu。_csdn局域网桌面控制ubuntu
文章浏览阅读1.6k次。表分区是解决一些因单表过大引用的性能问题的方式,比如某张表过大就会造成查询变慢,可能分区是一种解决方案。一般建议当单表大小超过内存就可以考虑表分区了。1,继承式分区,分为触发器(trigger)和规则(rule)两种方式触发器的方式1)创建表CREATE TABLE "public"."track_info_trigger_partition" ( "id" serial, "object_type" int2 NOT NULL DEFAULT 0, "object_name..._pg数据表分区的实现
文章浏览阅读3.3k次。物联网平台开源的有几个,就我晓得的有、、thingskit、JetLink、DG-iot(还有其他开源的,欢迎在评论区留言哦!),然后重点分析了下ThingsBoard、ThingsPanel和JetLink,ThingsBoard和Jetlinks是工程师思维产品,可以更多的通过配置去实现开发的目的,ThingsPanel是业务人员思路产品,或者开发或者用,避免了复杂的配置带来的较高学习门槛。ThingsBoard和Jetlinks是Java技术体系的,ThingsPanel是PHP开发的。_jetlinks和thingsboard
文章浏览阅读3.8k次。PostgreSQL 数据类型转换_pgsql数字转字符串
文章浏览阅读7k次,点赞3次,收藏14次。在做数据统计页面时,总会遇到统计某段时间内,每天、每月、每年的数据视图(柱状图、折线图等)。这些统计数据一眼看过去也简单呀,不就是按照时间周期(天、月、年)对统计数据进行分个组就完了嘛?但是会有一个问题,简单的写个sql对周期分组,获取到的统计数据是缺失的,即没有数据的那天,整条记录也都没有了。如下图需求:以当前月份(2023年2月)为起点,往后倒推一年,查询之前一年里每个月的统计数据。可见图中的数据其实是缺少的,这条sql只查询到了有数据的月份(23年的1月、2月,22年的12月)_如何用一条sql查出按年按月按天的汇总
文章浏览阅读3.8k次,点赞66次,收藏51次。PostgreSQL全球开发小组与2022年10月13日,宣布发布PostgreSQL15,这是世界上最先进的开源数据库的最新版本_mysql8 postgresql15
文章浏览阅读1.3k次。上文介绍了磁盘管理器中VFD的实现原理,本篇将从上层角度讲解磁盘管理器的工作细节。_smgrrelationdata
文章浏览阅读1.1k次。PostgreSQL设置中文语言界面和局域网访问_postgressql汉化
文章浏览阅读4.2k次。PostgreSQL 修改数据存储路径_如何设置postgresql 数据目录
文章浏览阅读4.7k次。在项目中用到了多数据源,在连接postgres数据库时,项目启动报错,说数据库连接错误,说dual不存在,网上好多教程都是说数据库查询的时候的大小写问题,而这个仅仅是连接,咋鞥却处理方法是修改application-dev.yml中的配置文件.项目中的druid参数是这样的:确实在配置文件中有个查询语句。_relation "dual" does not exist
文章浏览阅读4.9k次。PostgreSQL是一款强大的关系型数据库,但在实际使用过程中,许多用户经常会遇到慢SQL的问题。这些问题不仅会降低数据库性能,还会直接影响业务流程和用户体验。因此,本文将会深入分析PostgreSQL慢SQL的原因和优化方案,帮助用户更好地利用这个优秀的数据库系统。无论你是初学者还是专业开发者,本文都将为你提供实用的技巧和方法,让你的PostgreSQL数据库始终保持高效快速。_postgresql数据库优化
文章浏览阅读1.6k次。Linux配置postgresql开机自启_linux 启动pgsql
文章浏览阅读2k次。本篇介绍如何在centos7系统搭建一个postgresql主备集群实现最近的HA(高可用)架构。后续更高级的HA模式都是基于这个最基本的主备搭建。_postgresql主备