Skip to content

Commit 9cd5ef4

Browse files
committed
contrib/pgcrypto
1 parent d8ef285 commit 9cd5ef4

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type {
2+
Extension,
3+
ExtensionSetupResult,
4+
PGliteInterface,
5+
} from "../interface";
6+
7+
const setup = async (_pg: PGliteInterface, _emscriptenOpts: any) => {
8+
return {
9+
bundlePath: new URL("../../release/pgcrypto.tar.gz", import.meta.url),
10+
} satisfies ExtensionSetupResult;
11+
};
12+
13+
export const pgcrypto = {
14+
name: "pgcrypto",
15+
setup,
16+
} satisfies Extension;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { PGlite } from '../../dist/index.js'
3+
import { pgcrypto } from '../../dist/contrib/pgcrypto.js'
4+
5+
describe('pg_pgcryptotrgm', () => {
6+
it('digest', async () => {
7+
const pg = new PGlite({
8+
extensions: {
9+
pgcrypto,
10+
},
11+
})
12+
13+
await pg.exec('CREATE EXTENSION IF NOT EXISTS pgcrypto;')
14+
15+
const res = await pg.query(
16+
"SELECT encode(digest(convert_to('test', 'UTF8'), 'sha1'), 'hex') as value;",
17+
)
18+
expect(res.rows[0].value, 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3')
19+
})
20+
21+
it('hmac', async () => {
22+
const pg = new PGlite({
23+
extensions: {
24+
pgcrypto,
25+
},
26+
})
27+
28+
await pg.exec('CREATE EXTENSION IF NOT EXISTS pgcrypto;')
29+
30+
const res = await pg.query(
31+
"SELECT encode(hmac(convert_to('test', 'UTF8'), convert_to('key', 'UTF8'), 'sha1'), 'hex') as value;",
32+
)
33+
expect(res.rows[0].value).toEqual(
34+
'671f54ce0c540f78ffe1e26dcf9c2a047aea4fda',
35+
)
36+
})
37+
38+
it('crypt', async () => {
39+
const pg = new PGlite({
40+
extensions: {
41+
pgcrypto,
42+
},
43+
})
44+
45+
await pg.exec('CREATE EXTENSION IF NOT EXISTS pgcrypto;')
46+
47+
const res = await pg.query("SELECT crypt('test', gen_salt('bf')) as value;")
48+
expect(res.rows[0].value.length).toEqual(60)
49+
})
50+
51+
it('gen_salt', async () => {
52+
const pg = new PGlite({
53+
extensions: {
54+
pgcrypto,
55+
},
56+
})
57+
58+
await pg.exec('CREATE EXTENSION IF NOT EXISTS pgcrypto;')
59+
60+
const res = await pg.query("SELECT gen_salt('bf') as value;")
61+
expect(res.rows[0].value.length).toEqual(29)
62+
})
63+
64+
it('pgp_sym_encrypt and pgp_sym_decrypt', async () => {
65+
const pg = new PGlite({
66+
extensions: {
67+
pgcrypto,
68+
},
69+
})
70+
71+
await pg.exec('CREATE EXTENSION IF NOT EXISTS pgcrypto;')
72+
73+
const res = await pg.query(
74+
"SELECT pgp_sym_encrypt('test', 'key') as value;",
75+
)
76+
const encrypted = res.rows[0].value
77+
78+
const res2 = await pg.query("SELECT pgp_sym_decrypt($1, 'key') as value;", [
79+
encrypted,
80+
])
81+
expect(res2.rows[0].value).toEqual('test')
82+
})
83+
})
84+
85+
// TODO: pgp_pub_encrypt and pgp_pub_decrypt

0 commit comments

Comments
 (0)