Added decrypt

This commit is contained in:
Zam
2024-05-01 16:10:00 +02:00
parent e4c9149063
commit dc7bdd3bfd
2 changed files with 60 additions and 21 deletions

View File

@@ -12,9 +12,46 @@ 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 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 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 { pub fn encrypt(path: []const u8, key: [32]u8, nonce: [24]u8) !void {
{
var file = try std.fs.cwd().openFile(path, .{}); var file = try std.fs.cwd().openFile(path, .{});
defer file.close(); defer file.close();
var out_buf: [512]u8 = undefined; var out_buf: [512]u8 = undefined;
@@ -41,4 +78,6 @@ pub fn encrypt(path: []const u8, key: [32]u8, nonce: [24]u8) !void {
counter += 1; counter += 1;
_ = try out_file.write(out_buf[0..read]); _ = try out_file.write(out_buf[0..read]);
} }
}
try std.fs.cwd().deleteFile(path);
} }

Binary file not shown.