|
| 1 | +import test from "ava"; |
| 2 | +import { PGlite } from "../../dist/index.js"; |
| 3 | +import { pgcrypto } from "../../dist/contrib/pgcrypto.js"; |
| 4 | + |
| 5 | +test("pgcrypto digest", async (t) => { |
| 6 | + const pg = new PGlite({ |
| 7 | + extensions: { |
| 8 | + pgcrypto, |
| 9 | + }, |
| 10 | + }); |
| 11 | + |
| 12 | + await pg.exec("CREATE EXTENSION IF NOT EXISTS pgcrypto;"); |
| 13 | + |
| 14 | + const res = await pg.query("SELECT encode(digest(convert_to('test', 'UTF8'), 'sha1'), 'hex') as value;"); |
| 15 | + t.is(res.rows[0].value, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); |
| 16 | +}); |
| 17 | + |
| 18 | +test("pgcrypto hmac", async (t) => { |
| 19 | + const pg = new PGlite({ |
| 20 | + extensions: { |
| 21 | + pgcrypto, |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + await pg.exec("CREATE EXTENSION IF NOT EXISTS pgcrypto;"); |
| 26 | + |
| 27 | + const res = await pg.query("SELECT encode(hmac(convert_to('test', 'UTF8'), convert_to('key', 'UTF8'), 'sha1'), 'hex') as value;"); |
| 28 | + t.is(res.rows[0].value, "671f54ce0c540f78ffe1e26dcf9c2a047aea4fda"); |
| 29 | +}); |
| 30 | + |
| 31 | +test("pgcrypto crypt", async (t) => { |
| 32 | + const pg = new PGlite({ |
| 33 | + extensions: { |
| 34 | + pgcrypto, |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + await pg.exec("CREATE EXTENSION IF NOT EXISTS pgcrypto;"); |
| 39 | + |
| 40 | + const res = await pg.query("SELECT crypt('test', gen_salt('bf')) as value;"); |
| 41 | + t.is(res.rows[0].value.length, 60); |
| 42 | +}); |
| 43 | + |
| 44 | +test("pgcrypto gen_salt", async (t) => { |
| 45 | + const pg = new PGlite({ |
| 46 | + extensions: { |
| 47 | + pgcrypto, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + await pg.exec("CREATE EXTENSION IF NOT EXISTS pgcrypto;"); |
| 52 | + |
| 53 | + const res = await pg.query("SELECT gen_salt('bf') as value;"); |
| 54 | + t.is(res.rows[0].value.length, 29); |
| 55 | +}); |
| 56 | + |
| 57 | +test("pgcrypto pgp_sym_encrypt and pgp_sym_decrypt", async (t) => { |
| 58 | + const pg = new PGlite({ |
| 59 | + extensions: { |
| 60 | + pgcrypto, |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + await pg.exec("CREATE EXTENSION IF NOT EXISTS pgcrypto;"); |
| 65 | + |
| 66 | + const res = await pg.query("SELECT pgp_sym_encrypt('test', 'key') as value;"); |
| 67 | + const encrypted = res.rows[0].value; |
| 68 | + |
| 69 | + const res2 = await pg.query("SELECT pgp_sym_decrypt($1, 'key') as value;", [encrypted]); |
| 70 | + t.is(res2.rows[0].value, "test"); |
| 71 | +}); |
| 72 | + |
| 73 | +// TODO: pgp_pub_encrypt and pgp_pub_decrypt |
0 commit comments