diff --git a/set1/challenge5_repeating_xor/5.txt b/set1/challenge5_repeating_xor/5.txt new file mode 100644 index 0000000..ad63bd8 --- /dev/null +++ b/set1/challenge5_repeating_xor/5.txt @@ -0,0 +1,2 @@ +Burning 'em, if you ain't quick and nimble +I go crazy when I hear a cymbal diff --git a/set1/challenge5_repeating_xor/build.zig b/set1/challenge5_repeating_xor/build.zig new file mode 100644 index 0000000..1c13e58 --- /dev/null +++ b/set1/challenge5_repeating_xor/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("challenge5_repeating_xor", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/set1/challenge5_repeating_xor/src/main.zig b/set1/challenge5_repeating_xor/src/main.zig new file mode 100644 index 0000000..5d5bac4 --- /dev/null +++ b/set1/challenge5_repeating_xor/src/main.zig @@ -0,0 +1,21 @@ +const std = @import("std"); + +pub fn main() anyerror!void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader(); + const stdout = std.io.getStdOut().writer(); + + const key = "ICE"; + const input = try stdin.readAllAlloc(allocator, 4096); + defer allocator.free(input); + + var i: usize = 0; + for (input) |*char| { + char.* ^= key[i]; + i = (i + 1) % key.len; + } + + try stdout.print("{s}\n", .{std.fmt.fmtSliceHexLower(input)}); +}