Commit 461a554
2022-02-10 02:32:58
Changed files (1)
ps
ps/ps.zig
@@ -0,0 +1,51 @@
+// TODO:
+// - [ ] xPrint
+// - [ ] generate key at comp time
+
+const std = @import("std");
+const key: []const u8 = "ABCDEFG";
+
+// Protected String Namespace
+pub const PS = struct {
+ pub fn xAlloc(comptime str: []const u8, allocator: std.mem.Allocator) ![]const u8 {
+ const memory = try allocator.alloc(u8, str.len);
+ return xBuf(str, memory);
+ }
+
+ pub fn x(comptime str: []const u8) []const u8 {
+ comptime {
+ var buf: [str.len:0]u8 = undefined;
+ return xBuf(str, buf[0..]);
+ }
+ }
+
+ fn xBuf(comptime str: []const u8, buf: []u8) []const u8 {
+ for (str) |_, i| {
+ buf[i] = str[i] ^ key[i % key.len];
+ }
+ return buf;
+ }
+};
+
+test "protect string" {
+ const a = "flag{XoRf0rTh3W1n!!}";
+ const b = comptime PS.x("flag{XoRf0rTh3W1n!!}");
+ const res = try PS.xAlloc(b, std.testing.allocator);
+ defer std.testing.allocator.free(res);
+ try std.testing.expectEqualStrings(a, res);
+}
+
+const print = @import("std").debug.print;
+pub fn main() !void {
+ var buffer: [2048]u8 = undefined;
+ var fba = std.heap.FixedBufferAllocator.init(&buffer);
+ const allocator = fba.allocator();
+
+ const a = "flag{boocleartext}";
+ const b = comptime PS.x("flag{XoRf0rTh3W1n!!}");
+ const flag = try PS.xAlloc(b, allocator);
+ defer allocator.free(flag);
+
+ print("a: {s}\n", .{a});
+ print("b: {s}\n", .{flag});
+}