main
Raw Download raw file
 1// TODO:
 2//  - [ ] xPrint
 3//  - [ ] generate key at comp time
 4
 5const std = @import("std");
 6const key: []const u8 = "ABCDEFG";
 7
 8// Protected String Namespace
 9pub const PS = struct {
10    pub fn xAlloc(comptime str: []const u8, allocator: std.mem.Allocator) ![]const u8 {
11        const memory = try allocator.alloc(u8, str.len);
12        return xBuf(str, memory);
13    }
14
15    pub fn x(comptime str: []const u8) []const u8 {
16        comptime {
17            var buf: [str.len:0]u8 = undefined;
18            return xBuf(str, buf[0..]);
19        }
20    }
21
22    fn xBuf(comptime str: []const u8, buf: []u8) []const u8 {
23        for (str) |_, i| {
24            buf[i] = str[i] ^ key[i % key.len];
25        }
26        return buf;
27    }
28};
29
30test "protect string" {
31    const a = "flag{XoRf0rTh3W1n!!}";
32    const b = comptime PS.x("flag{XoRf0rTh3W1n!!}");
33    const res = try PS.xAlloc(b, std.testing.allocator);
34    defer std.testing.allocator.free(res);
35    try std.testing.expectEqualStrings(a, res);
36}
37
38const print = @import("std").debug.print;
39pub fn main() !void {
40    var buffer: [2048]u8 = undefined;
41    var fba = std.heap.FixedBufferAllocator.init(&buffer);
42    const allocator = fba.allocator();
43
44    const a = "flag{boocleartext}";
45    const b = comptime PS.x("flag{XoRf0rTh3W1n!!}");
46    const flag = try PS.xAlloc(b, allocator);
47    defer allocator.free(flag);
48
49    print("a: {s}\n", .{a});
50    print("b: {s}\n", .{flag});
51}