matasano/src/main.zig

76 lines
2.2 KiB
Zig
Raw Normal View History

2025-02-07 21:35:45 +01:00
const std = @import("std");
const b64 = @import("base64.zig");
const hex = @import("hex.zig");
2025-02-08 06:33:30 +01:00
const xor = @import("xor.zig");
2025-02-08 08:10:46 +01:00
const xor_crack = @import("xor_crack.zig");
2025-02-07 21:35:45 +01:00
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
const stdout = std.io.getStdOut().writer();
if (std.mem.eql(u8, args[1], "b64")) {
if (std.mem.eql(u8, args[2], "-d")) {
const result = try b64.decode(allocator, args[3]);
defer allocator.free(result);
try stdout.print("{s}", .{result});
}
if (std.mem.eql(u8, args[2], "-e")) {
const buf = try hex.decode(allocator, args[3]);
defer allocator.free(buf);
const result = try b64.encode(allocator, buf);
defer allocator.free(result);
try stdout.print("{s}", .{result});
}
}
2025-02-08 06:33:30 +01:00
if (std.mem.eql(u8, args[1], "xor")) {
const in_a = args[2];
const in_b = args[3];
const buf_a = try hex.decode(allocator, in_a);
defer allocator.free(buf_a);
const buf_b = try hex.decode(allocator, in_b);
defer allocator.free(buf_b);
const out = try xor.xor_buffers(allocator, buf_a, buf_b);
defer allocator.free(out);
const out_hex = try hex.encode(allocator, out);
defer allocator.free(out_hex);
try stdout.print("{s}", .{out_hex});
}
2025-02-08 08:10:46 +01:00
if (std.mem.eql(u8, args[1], "crack-xor")) {
const in_a = args[2];
const buf_a = try hex.decode(allocator, in_a);
defer allocator.free(buf_a);
const oracle = xor_crack.Oracle{
.decrypt = xor.xor_byte_noalloc,
};
const res = try xor_crack.single(allocator, buf_a, oracle) orelse {
try stdout.print("Did not find a solution", .{});
return;
};
try stdout.print("Found solution key={c}, score={d}\n", .{ res[0], res[1] });
const out = try xor.xor_byte(allocator, buf_a, res[0]);
defer allocator.free(out);
try stdout.print("{s}\n", .{out});
}
2025-02-07 21:35:45 +01:00
}