feat: Created own age wrapper.

This commit is contained in:
2026-04-28 18:03:06 -04:00
parent 0db97b714f
commit 3fd3ca2b10
11 changed files with 213 additions and 5 deletions

133
src/age.zig Normal file
View File

@@ -0,0 +1,133 @@
const std = @import("std");
/// Returns the decrypted contents of the file.
/// Caller is responsible for freeing the memory.
pub fn decrypt(
io: std.Io,
gpa: std.mem.Allocator,
private_key: []const u8,
input_path: []const u8,
output_path: []const u8,
) !void {
const result = try std.process.run(gpa, io, .{
.argv = &.{
"age",
"-d",
"-i",
private_key,
"-o",
output_path,
input_path,
},
});
defer gpa.free(result.stderr);
defer gpa.free(result.stdout);
if (result.stdout.len > 0) {
std.debug.print("stdout: \"{s}\"\n", .{result.stdout});
unreachable;
}
if (result.stderr.len > 0) {
std.debug.print("stderr: \"{s}\"\n", .{result.stderr});
unreachable;
}
}
/// Returns the encrypted contents of the file.
/// Caller is responsible for freeing the memory.
pub fn encrypt(
io: std.Io,
gpa: std.mem.Allocator,
public_key: []const u8,
input_path: []const u8,
output_path: []const u8,
) !void {
const result = try std.process.run(gpa, io, .{
.argv = &.{
"age",
"-e",
"-R",
public_key,
"-o",
output_path,
input_path,
},
});
defer gpa.free(result.stderr);
defer gpa.free(result.stdout);
if (result.stdout.len > 0) {
std.debug.print("stdout: \"{s}\"\n", .{result.stdout});
unreachable;
}
if (result.stderr.len > 0) {
std.debug.print("stderr: \"{s}\"\n", .{result.stderr});
unreachable;
}
}
test "sample file can be decrypted" {
const io = std.testing.io;
const gpa = std.testing.allocator;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmp.dir.realPathFileAlloc(io, ".", gpa);
defer gpa.free(dir_path);
const output_path = try std.fs.path.join(gpa, &.{ dir_path, "got.txt" });
defer gpa.free(output_path);
try decrypt(
io,
gpa,
"./fixtures/insecure-test-key",
"./fixtures/hello-world.age",
output_path,
);
const contents = try tmp.dir.readFileAlloc(io, output_path, gpa, .unlimited);
defer gpa.free(contents);
try std.testing.expectEqualSlices(u8, "Hello, World!\n", contents);
}
test "sample file can be encrypted" {
const io = std.testing.io;
const gpa = std.testing.allocator;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmp.dir.realPathFileAlloc(io, ".", gpa);
defer gpa.free(dir_path);
const output_path = try std.fs.path.join(gpa, &.{ dir_path, "hello-world.age" });
defer gpa.free(output_path);
try encrypt(
io,
gpa,
"./fixtures/insecure-test-key.pub",
"./fixtures/hello-world.txt",
output_path,
);
const got = try tmp.dir.readFileAlloc(io, output_path, gpa, .unlimited);
defer gpa.free(got);
const want = try std.Io.Dir.cwd().readFileAlloc(
io,
"./fixtures/hello-world.age",
gpa,
.unlimited,
);
const contents = try tmp.dir.readFileAlloc(io, output_path, gpa, .unlimited);
defer gpa.free(contents);
try std.testing.expectEqualSlices(u8, want, got);
}

View File

@@ -1,6 +1,12 @@
const std = @import("std");
const sqlite = @import("sqlite");
const age = @import("age.zig");
test {
std.testing.refAllDecls(@import("age.zig"));
}
test "simple database can be opened" {
var db = try sqlite.Db.init(.{
.mode = sqlite.Db.Mode{ .File = "./fixtures/example.db" },
@@ -24,3 +30,47 @@ test "simple database can be opened" {
return error.TestUnexpectedResult;
}
}
test "encrypted database can be opened" {
const io = std.testing.io;
const gpa = std.testing.allocator;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const dir_path = try tmp.dir.realPathFileAlloc(io, ".", gpa);
defer gpa.free(dir_path);
const decrypted_path = try std.fs.path.joinZ(gpa, &.{ dir_path, "example.db" });
defer gpa.free(decrypted_path);
try age.decrypt(
io,
gpa,
"./fixtures/insecure-test-key",
"./fixtures/encrypted-example.db.age",
decrypted_path,
);
var db = try sqlite.Db.init(.{
.mode = sqlite.Db.Mode{ .File = decrypted_path },
.open_flags = .{
.write = false,
.create = false,
},
.threading_mode = .MultiThread,
});
var stmt = try db.prepare("SELECT * FROM hello");
defer stmt.deinit();
const alloc = std.testing.allocator;
if (try stmt.oneAlloc(struct { text: []const u8 }, alloc, .{}, .{})) |got| {
defer alloc.free(got.text);
try std.testing.expectEqualSlices(u8, "world!", got.text);
} else {
return error.TestUnexpectedResult;
}
}