From dc7bdd3bfdb68b3a6aa79fd420ec4c932b3c9ee4 Mon Sep 17 00:00:00 2001 From: Zam Date: Wed, 1 May 2024 16:10:00 +0200 Subject: [PATCH] Added decrypt --- src/main.zig | 81 ++++++++++++++++++++++++++++++++++++++------------- test.txt.cha | Bin 303 -> 0 bytes 2 files changed, 60 insertions(+), 21 deletions(-) delete mode 100644 test.txt.cha diff --git a/src/main.zig b/src/main.zig index a01f63a..495779a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -12,33 +12,72 @@ pub fn main() !void { try crypto.pwhash.argon2.kdf(allocator, &key, pwd, &nonce, crypto.pwhash.argon2.Params.interactive_2id, crypto.pwhash.argon2.Mode.argon2id); try encrypt("test.txt", key, nonce); + try decrypt(allocator, "test.txt.cha", pwd[0..]); +} + +pub fn decrypt(allocator: std.mem.Allocator, path: []const u8, pwd: []const u8) !void { + { + var out_buf: [512]u8 = undefined; + var in_buf: [512]u8 = undefined; + var nonce: [24]u8 = undefined; + var key: [32]u8 = undefined; + + var file = try std.fs.cwd().openFile(path, .{}); + defer file.close(); + + _ = try file.read(&nonce); + + try crypto.pwhash.argon2.kdf(allocator, &key, pwd, &nonce, crypto.pwhash.argon2.Params.interactive_2id, crypto.pwhash.argon2.Mode.argon2id); + var out_file = try std.fs.cwd().createFile(path[0 .. path.len - 4], .{ + .read = true, + .truncate = true, + }); + defer out_file.close(); + + var counter: u32 = 0; + var read: usize = 0; + while (true) { + read = try file.read(&in_buf); + if (read <= 0) { + break; + } + + crypto.stream.chacha.XChaCha20IETF.xor(out_buf[0..read], in_buf[0..read], counter, key, nonce); + counter += 1; + _ = try out_file.write(out_buf[0..read]); + } + } + try std.fs.cwd().deleteFile(path); } pub fn encrypt(path: []const u8, key: [32]u8, nonce: [24]u8) !void { - var file = try std.fs.cwd().openFile(path, .{}); - defer file.close(); - var out_buf: [512]u8 = undefined; - var in_buf: [512]u8 = undefined; + { + var file = try std.fs.cwd().openFile(path, .{}); + defer file.close(); + var out_buf: [512]u8 = undefined; + var in_buf: [512]u8 = undefined; - // missuse out_buf as buffer for format string - _ = try std.fmt.bufPrint(out_buf[0 .. path.len + 4], "{s}.cha", .{path}); - var out_file = try std.fs.cwd().createFile(out_buf[0 .. path.len + 4], .{ - .read = true, - }); - defer out_file.close(); + // missuse out_buf as buffer for format string + _ = try std.fmt.bufPrint(out_buf[0 .. path.len + 4], "{s}.cha", .{path}); + var out_file = try std.fs.cwd().createFile(out_buf[0 .. path.len + 4], .{ + .read = true, + }); + defer out_file.close(); - _ = try out_file.write(&nonce); + _ = try out_file.write(&nonce); - var counter: u32 = 0; - var read: usize = 0; - while (true) { - read = try file.read(&in_buf); - if (read <= 0) { - break; + var counter: u32 = 0; + var read: usize = 0; + while (true) { + read = try file.read(&in_buf); + if (read <= 0) { + break; + } + + crypto.stream.chacha.XChaCha20IETF.xor(out_buf[0..read], in_buf[0..read], counter, key, nonce); + counter += 1; + _ = try out_file.write(out_buf[0..read]); } - - crypto.stream.chacha.XChaCha20IETF.xor(out_buf[0..read], in_buf[0..read], counter, key, nonce); - counter += 1; - _ = try out_file.write(out_buf[0..read]); } + try std.fs.cwd().deleteFile(path); } diff --git a/test.txt.cha b/test.txt.cha deleted file mode 100644 index 54245d38792542aa471b0083cda279706bf6fc5d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 303 zcmV+~0nq;X`74rad9k5;kf|=gzF&2JEw^NraVkdmlo$cgta8EL1ESNBJcL8oEz%Au>$yD zz#E)O7WWWr+slw=iBK{ElDSpxY@y|=ZLQ!>n2mLY!j4)j^vzk$o&Iq>Ff5Dg4H$CR zcqC6+;$qE`^#r5f{88`A2)i>$QqNBnfK8AhR@hYrZ6Sm1>Y6ItLX62og+#m~j1B$F BnE3zz