summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--examples/zigcrypto.zig59
1 files changed, 59 insertions, 0 deletions
diff --git a/examples/zigcrypto.zig b/examples/zigcrypto.zig
new file mode 100644
index 0000000..05c44a5
--- /dev/null
+++ b/examples/zigcrypto.zig
@@ -0,0 +1,59 @@
1const std = @import("std");
2const Blake3 = std.crypto.hash.Blake3;
3const Sha3_512 = std.crypto.hash.sha3.Sha3_512;
4
5const sqlite = @import("sqlite");
6const c = sqlite.c;
7
8const name = "zigcrypto";
9
10pub const loadable_extension = true;
11
12var module_allocator: std.heap.GeneralPurposeAllocator(.{}) = undefined;
13var module_context: sqlite.vtab.ModuleContext = undefined;
14
15const logger = std.log.scoped(.zigcrypto);
16
17fn createAllFunctions(db: *sqlite.Db) !void {
18 try db.createScalarFunction(
19 "blake3",
20 struct {
21 fn run(input: []const u8) [Blake3.digest_length]u8 {
22 var output: [Blake3.digest_length]u8 = undefined;
23 Blake3.hash(input, output[0..], .{});
24 return output;
25 }
26 }.run,
27 .{},
28 );
29 try db.createScalarFunction(
30 "sha3_512",
31 struct {
32 fn run(input: []const u8) [Sha3_512.digest_length]u8 {
33 var output: [Sha3_512.digest_length]u8 = undefined;
34 Sha3_512.hash(input, output[0..], .{});
35 return output;
36 }
37 }.run,
38 .{},
39 );
40}
41
42pub export fn sqlite3_zigcrypto_init(raw_db: *c.sqlite3, err_msg: [*c][*c]u8, api: *c.sqlite3_api_routines) callconv(.C) c_int {
43 _ = err_msg;
44
45 c.sqlite3_api = api;
46
47 module_allocator = std.heap.GeneralPurposeAllocator(.{}){};
48
49 var db = sqlite.Db{
50 .db = raw_db,
51 };
52
53 createAllFunctions(&db) catch |err| {
54 logger.err("unable to create all SQLite functions, err: {!}", .{err});
55 return c.SQLITE_ERROR;
56 };
57
58 return c.SQLITE_OK;
59}