Change from folder Encryptor to single file en/decryptor

This commit is contained in:
Pablu23
2024-05-25 02:20:36 +02:00
parent 9d01d01a70
commit 2fc8b8cffa
2 changed files with 27 additions and 39 deletions

View File

@@ -3,58 +3,46 @@ const crypto = std.crypto;
pub fn main() !void { pub fn main() !void {
const allocator = std.heap.page_allocator; const allocator = std.heap.page_allocator;
const pwd = "Test123!"; const stdin = std.io.getStdIn().reader();
var buf: [50]u8 = std.mem.zeroes([50]u8);
const input = try stdin.readUntilDelimiterOrEof(buf[0..], '\n') orelse return;
const pwd = std.mem.trimRight(u8, input[0 .. input.len - 1], "\r");
const dir = try std.fs.cwd().openDir("./test", .{ .iterate = true }); var should_encrypt = false;
if (dir.openDir("private", .{ .iterate = true })) |priv| { var args = try std.process.argsWithAllocator(allocator);
var walker = try priv.walk(allocator);
defer walker.deinit();
while (try walker.next()) |f| { _ = args.skip();
if (f.kind == std.fs.Dir.Entry.Kind.file) { const dir = std.fs.cwd();
decrypt(allocator, priv, f.path, pwd) catch |err| std.debug.print("{?}\n", .{err}); while (args.next()) |arg| {
if (std.mem.eql(u8, arg, "encrypt")) {
should_encrypt = true;
} else if (std.mem.eql(u8, arg, "decrypt")) {
should_encrypt = false;
} else {
if (should_encrypt) {
var key: [32]u8 = std.mem.zeroes([32]u8);
var nonce: [24]u8 = undefined;
crypto.random.bytes(&nonce);
try crypto.pwhash.argon2.kdf(allocator, &key, pwd, &nonce, crypto.pwhash.argon2.Params.interactive_2id, crypto.pwhash.argon2.Mode.argon2id);
try encrypt(dir, arg, key, nonce);
} else {
try decrypt(allocator, dir, arg, pwd);
} }
} }
try dir.deleteDir("private");
} else |err| switch (err) {
std.fs.Dir.OpenError.FileNotFound => {
var key: [32]u8 = std.mem.zeroes([32]u8);
var nonce: [24]u8 = undefined;
crypto.random.bytes(&nonce);
try crypto.pwhash.argon2.kdf(allocator, &key, pwd, &nonce, crypto.pwhash.argon2.Params.interactive_2id, crypto.pwhash.argon2.Mode.argon2id);
try dir.makeDir("private");
var walker = try dir.walk(allocator);
defer walker.deinit();
while (try walker.next()) |f| {
if (f.kind == std.fs.Dir.Entry.Kind.file) {
std.debug.print("Encrypting {s}\n", .{f.path});
encrypt(dir, f.path, key, nonce) catch |e| std.debug.print("{?}\n", .{e});
}
}
},
else => {
std.debug.print("Err: {?}\n", .{err});
return;
},
} }
} }
pub fn decrypt(allocator: std.mem.Allocator, priv: std.fs.Dir, path: []const u8, pwd: []const u8) !void { pub fn decrypt(allocator: std.mem.Allocator, dir: std.fs.Dir, path: []const u8, pwd: []const u8) !void {
{ {
var out_buf: [512]u8 = undefined; var out_buf: [512]u8 = undefined;
var in_buf: [512]u8 = undefined; var in_buf: [512]u8 = undefined;
var nonce: [24]u8 = undefined; var nonce: [24]u8 = undefined;
var key: [32]u8 = undefined; var key: [32]u8 = undefined;
var file = try priv.openFile(path, .{}); var file = try dir.openFile(path, .{});
defer file.close(); defer file.close();
_ = try file.read(&nonce); _ = try file.read(&nonce);
var dir = try priv.openDir("..", .{});
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);
var out_file = try dir.createFile(path[0 .. path.len - 4], .{ var out_file = try dir.createFile(path[0 .. path.len - 4], .{
@@ -76,7 +64,7 @@ pub fn decrypt(allocator: std.mem.Allocator, priv: std.fs.Dir, path: []const u8,
_ = try out_file.write(out_buf[0..read]); _ = try out_file.write(out_buf[0..read]);
} }
} }
try priv.deleteFile(path); try dir.deleteFile(path);
} }
pub fn encrypt(dir: std.fs.Dir, path: []const u8, key: [32]u8, nonce: [24]u8) !void { pub fn encrypt(dir: std.fs.Dir, path: []const u8, key: [32]u8, nonce: [24]u8) !void {
@@ -85,11 +73,10 @@ pub fn encrypt(dir: std.fs.Dir, path: []const u8, key: [32]u8, nonce: [24]u8) !v
defer file.close(); defer file.close();
var out_buf: [512]u8 = undefined; var out_buf: [512]u8 = undefined;
var in_buf: [512]u8 = undefined; var in_buf: [512]u8 = undefined;
var priv = try dir.openDir("private", .{});
// missuse out_buf as buffer for format string // missuse out_buf as buffer for format string
_ = try std.fmt.bufPrint(out_buf[0 .. path.len + 4], "{s}.cha", .{path}); _ = try std.fmt.bufPrint(out_buf[0 .. path.len + 4], "{s}.cha", .{path});
var out_file = try priv.createFile(out_buf[0 .. path.len + 4], .{ var out_file = try dir.createFile(out_buf[0 .. path.len + 4], .{
.read = true, .read = true,
}); });
defer out_file.close(); defer out_file.close();

1
testfile Normal file
View File

@@ -0,0 +1 @@
Hallo das ist eine Test datei um zu testen ob man sachen verschlüsseln kann oder nicht :D