2023-03-20 22:59:09 +00:00
|
|
|
import std.stdio;
|
|
|
|
import std.path;
|
|
|
|
import std.file;
|
|
|
|
import std.algorithm : endsWith;
|
|
|
|
import std.string : strip;
|
2023-03-21 07:48:12 +00:00
|
|
|
import std.getopt;
|
2023-03-20 22:59:09 +00:00
|
|
|
|
|
|
|
import botan.block.block_cipher : BlockCipher;
|
|
|
|
import botan.block.aes : AES256;
|
|
|
|
import botan.hash.hash : HashFunction;
|
|
|
|
import botan.hash.sha2_32 : SHA256;
|
|
|
|
|
|
|
|
import cipher_utils;
|
2023-03-21 11:53:18 +00:00
|
|
|
import cli_utils;
|
2023-03-20 22:59:09 +00:00
|
|
|
|
|
|
|
int main(string[] args) {
|
2023-03-21 07:48:12 +00:00
|
|
|
Params params;
|
|
|
|
int result = parseParams(args, params);
|
|
|
|
if (result != 0) {
|
|
|
|
printUsage();
|
|
|
|
return result;
|
2023-03-20 22:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockCipher cipher = null;
|
|
|
|
// TODO: Use args to determine block cipher.
|
2023-03-21 11:53:18 +00:00
|
|
|
writeln("Enter a passphrase:");
|
2023-03-21 07:48:12 +00:00
|
|
|
string password = readPassphrase();
|
|
|
|
if (password is null) {
|
|
|
|
return 2;
|
|
|
|
}
|
2023-03-20 22:59:09 +00:00
|
|
|
|
|
|
|
HashFunction hash = new SHA256();
|
|
|
|
auto secureKeyVector = hash.process(password);
|
|
|
|
cipher = new AES256();
|
|
|
|
cipher.setKey(secureKeyVector);
|
|
|
|
|
|
|
|
ubyte[] buffer = new ubyte[cipher.blockSize];
|
2023-03-21 07:48:12 +00:00
|
|
|
if (isDir(params.target)) {
|
|
|
|
if (params.action == Action.ENCRYPT) {
|
|
|
|
encryptDir(params.target, cipher, buffer, params.recursive);
|
2023-03-20 22:59:09 +00:00
|
|
|
} else {
|
2023-03-21 07:48:12 +00:00
|
|
|
decryptDir(params.target, cipher, buffer, params.recursive);
|
2023-03-20 22:59:09 +00:00
|
|
|
}
|
2023-03-21 07:48:12 +00:00
|
|
|
} else if (isFile(params.target)) {
|
|
|
|
if (params.action == Action.ENCRYPT) {
|
|
|
|
encryptAndRemoveFile(params.target, cipher, buffer);
|
2023-03-20 22:59:09 +00:00
|
|
|
} else {
|
2023-03-21 07:48:12 +00:00
|
|
|
decryptAndRemoveFile(params.target, cipher, buffer);
|
2023-03-20 22:59:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stderr.writeln("Target is not a directory or file.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-21 07:48:12 +00:00
|
|
|
|
2023-03-20 22:59:09 +00:00
|
|
|
void encryptAndRemoveFile(string filename, BlockCipher cipher, ref ubyte[] buffer) {
|
|
|
|
string encryptedFilename = filename ~ ".enc";
|
|
|
|
encryptFile(filename, encryptedFilename, cipher, buffer);
|
|
|
|
std.file.remove(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void decryptAndRemoveFile(string filename, BlockCipher cipher, ref ubyte[] buffer) {
|
|
|
|
string decryptedFilename = filename[0 .. $-4];
|
|
|
|
decryptFile(filename, decryptedFilename, cipher, buffer);
|
|
|
|
std.file.remove(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void encryptDir(string dirname, BlockCipher cipher, ref ubyte[] buffer, bool recursive) {
|
|
|
|
string[] dirsToTraverse;
|
|
|
|
foreach (DirEntry entry; dirEntries(dirname, SpanMode.shallow, false)) {
|
|
|
|
if (entry.isFile && !endsWith(entry.name, ".enc")) {
|
|
|
|
encryptAndRemoveFile(entry.name, cipher, buffer);
|
|
|
|
} else if (entry.isDir && recursive) {
|
|
|
|
dirsToTraverse ~= entry.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (recursive) {
|
|
|
|
foreach (string childDirname; dirsToTraverse) {
|
|
|
|
encryptDir(childDirname, cipher, buffer, recursive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void decryptDir(string dirname, BlockCipher cipher, ref ubyte[] buffer, bool recursive) {
|
|
|
|
string[] dirsToTraverse;
|
|
|
|
foreach (DirEntry entry; dirEntries(dirname, SpanMode.shallow, false)) {
|
|
|
|
if (entry.isFile && endsWith(entry.name, ".enc")) {
|
|
|
|
decryptAndRemoveFile(entry.name, cipher, buffer);
|
|
|
|
} else if (entry.isDir && recursive) {
|
|
|
|
dirsToTraverse ~= entry.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (recursive) {
|
|
|
|
foreach (string childDirname; dirsToTraverse) {
|
|
|
|
decryptDir(childDirname, cipher, buffer, recursive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|