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