Compare commits
4 Commits
a512694d11
...
master
Author | SHA1 | Date |
---|---|---|
Thomas Lindner | 377682bd1a | |
Thomas Lindner | e412d63914 | |
Thomas Lindner | 6abec1ca62 | |
Thomas Lindner | 55283f4376 |
|
@ -1,17 +1,15 @@
|
|||
const std = @import("std");
|
||||
|
||||
fn readHex(allocator: std.mem.Allocator, max_size: usize) ![]u8 {
|
||||
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
|
||||
|
||||
var inputbuffer = std.ArrayList(u8).init(allocator);
|
||||
defer inputbuffer.deinit();
|
||||
fn readHex(allocator: std.mem.Allocator, reader: anytype, max_size: usize) !?[]u8 {
|
||||
var resultbuffer = std.ArrayList(u8).init(allocator);
|
||||
defer resultbuffer.deinit();
|
||||
|
||||
while (true) {
|
||||
try stdin.readUntilDelimiterArrayList(&inputbuffer, '\n', max_size * 2);
|
||||
try resultbuffer.resize(inputbuffer.items.len / 2);
|
||||
_ = std.fmt.hexToBytes(resultbuffer.items, inputbuffer.items) catch {
|
||||
const line = try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', max_size * 2);
|
||||
const hex = line orelse return null;
|
||||
defer allocator.free(hex);
|
||||
try resultbuffer.resize(hex.len / 2);
|
||||
_ = std.fmt.hexToBytes(resultbuffer.items, hex) catch {
|
||||
std.log.err("not a valid hexadecimal", .{});
|
||||
continue;
|
||||
};
|
||||
|
@ -23,13 +21,14 @@ 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();
|
||||
|
||||
while (true) {
|
||||
const input1 = try readHex(allocator, 4096);
|
||||
const input1 = (try readHex(allocator, stdin, 4096)) orelse break;
|
||||
defer allocator.free(input1);
|
||||
|
||||
const input2 = try readHex(allocator, 4096);
|
||||
const input2 = (try readHex(allocator, stdin, 4096)) orelse break;
|
||||
defer allocator.free(input2);
|
||||
|
||||
if (input1.len != input2.len) {
|
||||
|
|
|
@ -1,17 +1,15 @@
|
|||
const std = @import("std");
|
||||
|
||||
fn readHex(allocator: std.mem.Allocator, max_size: usize) ![]u8 {
|
||||
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
|
||||
|
||||
var inputbuffer = std.ArrayList(u8).init(allocator);
|
||||
defer inputbuffer.deinit();
|
||||
fn readHex(allocator: std.mem.Allocator, reader: anytype, max_size: usize) !?[]u8 {
|
||||
var resultbuffer = std.ArrayList(u8).init(allocator);
|
||||
defer resultbuffer.deinit();
|
||||
|
||||
while (true) {
|
||||
try stdin.readUntilDelimiterArrayList(&inputbuffer, '\n', max_size * 2);
|
||||
try resultbuffer.resize(inputbuffer.items.len / 2);
|
||||
_ = std.fmt.hexToBytes(resultbuffer.items, inputbuffer.items) catch {
|
||||
const line = try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', max_size * 2);
|
||||
const hex = line orelse return null;
|
||||
defer allocator.free(hex);
|
||||
try resultbuffer.resize(hex.len / 2);
|
||||
_ = std.fmt.hexToBytes(resultbuffer.items, hex) catch {
|
||||
std.log.err("not a valid hexadecimal", .{});
|
||||
continue;
|
||||
};
|
||||
|
@ -42,10 +40,11 @@ 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();
|
||||
|
||||
while (true) {
|
||||
const ciphertext = try readHex(allocator, 4096);
|
||||
const ciphertext = (try readHex(allocator, stdin, 4096)) orelse break;
|
||||
defer allocator.free(ciphertext);
|
||||
|
||||
var plaintext = try allocator.alloc(u8, ciphertext.len);
|
||||
|
@ -53,7 +52,7 @@ pub fn main() anyerror!void {
|
|||
var bestkey: u8 = 0;
|
||||
var bestscore: u32 = 0;
|
||||
var key: u8 = 0;
|
||||
while (true) {
|
||||
while (true) : (key += 1) {
|
||||
decrypt(plaintext, ciphertext, key);
|
||||
const s = score(plaintext);
|
||||
if (s > bestscore) {
|
||||
|
@ -61,7 +60,7 @@ pub fn main() anyerror!void {
|
|||
bestscore = s;
|
||||
}
|
||||
key +%= 1;
|
||||
if (key == 0) {
|
||||
if (key == std.math.maxInt(u8)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,8 +43,8 @@ pub fn main() anyerror!void {
|
|||
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
var bestplaintext = try allocator.alloc(u8, 0);
|
||||
defer allocator.free(bestplaintext);
|
||||
var bestplaintext = std.ArrayList(u8).init(allocator);
|
||||
defer bestplaintext.deinit();
|
||||
var bestscore: u32 = 0;
|
||||
|
||||
while (true) {
|
||||
|
@ -54,21 +54,19 @@ pub fn main() anyerror!void {
|
|||
var plaintext = try allocator.alloc(u8, ciphertext.len);
|
||||
defer allocator.free(plaintext);
|
||||
var key: u8 = 0;
|
||||
while (true) {
|
||||
while (true) : (key += 1) {
|
||||
decrypt(plaintext, ciphertext, key);
|
||||
const s = score(plaintext);
|
||||
if (s > bestscore) {
|
||||
allocator.free(bestplaintext);
|
||||
bestplaintext = try allocator.alloc(u8, plaintext.len);
|
||||
std.mem.copy(u8, bestplaintext, plaintext);
|
||||
try bestplaintext.resize(plaintext.len);
|
||||
std.mem.copy(u8, bestplaintext.items, plaintext);
|
||||
bestscore = s;
|
||||
}
|
||||
key +%= 1;
|
||||
if (key == 0) {
|
||||
if (key == std.math.maxInt(u8)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try stdout.print("{s}\n", .{bestplaintext});
|
||||
try stdout.print("{s}\n", .{bestplaintext.items});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Burning 'em, if you ain't quick and nimble
|
||||
I go crazy when I hear a cymbal
|
|
@ -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);
|
||||
}
|
|
@ -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)});
|
||||
}
|
Loading…
Reference in New Issue