postgresql_16:9.6.28.2 Password Hashing Functions
PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > pgcrypto — cryptographic functions > Password Hashing Functions
The functions crypt() and gen_salt() are specifically designed for hashing passwords. crypt() does the hashing and gen_salt() prepares algorithm parameters for it.
The algorithms in crypt() differ from the usual MD5 or SHA1 hashing algorithms 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 the *salt*, 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.
(see pgcrypto-crypt-algorithms) lists the algorithms supported by the crypt() function.
| Algorithm | Max Password Length | Adaptive? | Salt Bits | Output Length | Description | | --- | --- | --- | --- | --- | --- | | bf | 72 | yes | 128 | 60 | Blowfish-based, variant 2a | | md5 | unlimited | no | 48 | 34 | MD5-based crypt | | xdes | 8 | yes | 24 | 20 | Extended DES | | des | 8 | no | 12 | 13 | Original UNIX crypt |
crypt()
crypt(password text, salt text) returns text
Calculates a crypt(3)-style hash of password. When storing a new password, you need to use gen_salt() to generate a new salt value. To check a password, pass the stored hash value as salt, 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)) AS pswmatch FROM ... ; This returns true if the entered password is correct.
gen_salt()
gen_salt(type text [, iter_count integer ]) returns text
Generates a new random salt string for use in crypt(). The salt string also tells crypt() which algorithm to use.
The type parameter specifies the hashing algorithm. The accepted types are: des, xdes, md5 and bf.
The iter_count parameter 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 the iter_count parameter is omitted, the default iteration count is used. Allowed values for iter_count depend on the algorithm and are shown in (see postgresql_16:9.6.28.2).
| Algorithm | Default | Min | Max | | --- | --- | --- | --- | | xdes | 725 | 1 | 16777215 | | bf | 6 | 4 | 31 |
For xdes there 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.
(see postgresql_16:9.6.28.2) 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 lower case letters, or upper- and lower-case letters and numbers. In the crypt-bf entries, the number after a slash is the iter_count parameter of gen_salt.
| Algorithm | Hashes/sec | For [a-z] | For [A-Za-z0-9] | Duration relative to md5 hash | | --- | --- | --- | --- | --- | | crypt-bf/8 | 1792 | 4 years | 3927 years | 100k | | crypt-bf/7 | 3648 | 2 years | 1929 years | 50k | | crypt-bf/6 | 7168 | 1 year | 982 years | 25k | | crypt-bf/5 | 13504 | 188 days | 521 years | 12.5k | | crypt-md5 | 171584 | 15 days | 41 years | 1k | | crypt-des | 23221568 | 157.5 minutes | 108 days | 7 | | sha1 | 37774272 | 90 minutes | 68 days | 4 | | md5 (hash) | 150085504 | 22.5 minutes | 17 days | 1 |
Notes:
- The machine used is an Intel Mobile Core i3.
crypt-desandcrypt-md5algorithm numbers are taken from John the Ripper v1.6.38-testoutput.md5 hashnumbers 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 the speed with different numbers of iterations can be shown. For reference:john -testshows 13506 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.