| 1 |
-- |
| 2 |
-- crypt() and gensalt: sha256crypt, sha512crypt |
| 3 |
-- |
| 4 |
|
| 5 |
-- $5$ is sha256crypt |
| 6 |
SELECT crypt('', '$5$Szzz0yzz'); |
| 7 |
|
| 8 |
SELECT crypt('foox', '$5$Szzz0yzz'); |
| 9 |
|
| 10 |
CREATE TABLE ctest (data text, res text, salt text); |
| 11 |
INSERT INTO ctest VALUES ('password', '', ''); |
| 12 |
|
| 13 |
-- generate a salt for sha256crypt, default rounds |
| 14 |
UPDATE ctest SET salt = gen_salt('sha256crypt'); |
| 15 |
UPDATE ctest SET res = crypt(data, salt); |
| 16 |
SELECT res = crypt(data, res) AS "worked" |
| 17 |
FROM ctest; |
| 18 |
|
| 19 |
-- generate a salt for sha256crypt, rounds 9999 |
| 20 |
UPDATE ctest SET salt = gen_salt('sha256crypt', 9999); |
| 21 |
UPDATE ctest SET res = crypt(data, salt); |
| 22 |
SELECT res = crypt(data, res) AS "worked" |
| 23 |
FROM ctest; |
| 24 |
|
| 25 |
-- should fail, below supported minimum rounds value |
| 26 |
UPDATE ctest SET salt = gen_salt('sha256crypt', 10); |
| 27 |
|
| 28 |
-- should fail, exceeds supported maximum rounds value |
| 29 |
UPDATE ctest SET salt = gen_salt('sha256crypt', 1000000000); |
| 30 |
|
| 31 |
TRUNCATE ctest; |
| 32 |
|
| 33 |
-- $6$ is sha512crypt |
| 34 |
SELECT crypt('', '$6$Szzz0yzz'); |
| 35 |
|
| 36 |
SELECT crypt('foox', '$6$Szzz0yzz'); |
| 37 |
|
| 38 |
INSERT INTO ctest VALUES ('password', '', ''); |
| 39 |
|
| 40 |
-- generate a salt for sha512crypt, default rounds |
| 41 |
UPDATE ctest SET salt = gen_salt('sha512crypt'); |
| 42 |
UPDATE ctest SET res = crypt(data, salt); |
| 43 |
SELECT res = crypt(data, res) AS "worked" |
| 44 |
FROM ctest; |
| 45 |
|
| 46 |
-- generate a salt for sha512crypt, rounds 9999 |
| 47 |
UPDATE ctest SET salt = gen_salt('sha512crypt', 9999); |
| 48 |
UPDATE ctest SET res = crypt(data, salt); |
| 49 |
SELECT res = crypt(data, res) AS "worked" |
| 50 |
FROM ctest; |
| 51 |
|
| 52 |
-- should fail, below supported minimum rounds value |
| 53 |
UPDATE ctest SET salt = gen_salt('sha512crypt', 10); |
| 54 |
|
| 55 |
-- should fail, exceeds supported maximum rounds value |
| 56 |
UPDATE ctest SET salt = gen_salt('sha512crypt', 1000000000); |
| 57 |
|
| 58 |
-- Extended tests taken from public domain code at |
| 59 |
-- https://www.akkadia.org/drepper/SHA-crypt.txt |
| 60 |
-- |
| 61 |
-- We adapt the tests defined there to make sure we are compatible with the reference |
| 62 |
-- implementation. |
| 63 |
|
| 64 |
-- This tests sha256crypt (magic byte $5$ with salt and rounds) |
| 65 |
SELECT crypt('Hello world!', '$5$saltstring') |
| 66 |
= '$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5' AS result; |
| 67 |
SELECT crypt('Hello world!', '$5$rounds=10000$saltstringsaltstring') |
| 68 |
= '$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2.opqey6IcA' AS result; |
| 69 |
SELECT crypt('This is just a test', '$5$rounds=5000$toolongsaltstring') |
| 70 |
= '$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8mGRcvxa5' AS result; |
| 71 |
SELECT crypt('a very much longer text to encrypt. This one even stretches over more' |
| 72 |
'than one line.', '$5$rounds=1400$anotherlongsaltstring') |
| 73 |
= '$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIUnzyxf12oP84Bnq1' AS result; |
| 74 |
SELECT crypt('we have a short salt string but not a short password', '$5$rounds=77777$short') |
| 75 |
= '$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0KQRd/' AS result; |
| 76 |
SELECT crypt('a short string', '$5$rounds=123456$asaltof16chars..') |
| 77 |
= '$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/cZKmF/wJvD' AS result; |
| 78 |
SELECT crypt('the minimum number is still observed', '$5$rounds=10$roundstoolow') |
| 79 |
= '$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY9l/gL972bIC' AS result; |
| 80 |
|
| 81 |
-- The following tests sha512crypt (magic byte $6$ with salt and rounds) |
| 82 |
SELECT crypt('Hello world!', '$6$saltstring') |
| 83 |
= '$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1' AS result; |
| 84 |
SELECT crypt('Hello world!', '$6$rounds=10000$saltstringsaltstring') |
| 85 |
= '$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v.' AS result; |
| 86 |
SELECT crypt('This is just a test', '$6$rounds=5000$toolongsaltstring') |
| 87 |
= '$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0' AS result; |
| 88 |
SELECT crypt('a very much longer text to encrypt. This one even stretches over more' |
| 89 |
'than one line.', '$6$rounds=1400$anotherlongsaltstring') |
| 90 |
= '$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wPvMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1' AS result; |
| 91 |
SELECT crypt('we have a short salt string but not a short password', '$6$rounds=77777$short') |
| 92 |
= '$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0gge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0' AS result; |
| 93 |
SELECT crypt('a short string', '$6$rounds=123456$asaltof16chars..') |
| 94 |
= '$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwcelCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1' AS result; |
| 95 |
SELECT crypt('the minimum number is still observed', '$6$rounds=10$roundstoolow') |
| 96 |
= '$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1xhLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX.' AS result; |
| 97 |
|
| 98 |
-- cleanup |
| 99 |
DROP TABLE ctest; |