Skip to content

Commit

Permalink
Add --create-key argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeliux committed Apr 13, 2024
1 parent d7253a8 commit e98af74
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/Kryptor.Tool/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ internal class Arguments
[Option('c', "continuous", HelpText = "Usable with -e or -d, Use more secure continuous encryption/decryption method.")]
public bool Continuous { get; set; }

[Option('C', "create-key", HelpText = "Usable with -e, Creates new keystore for each files.")]
public bool CreateKey { get; set; }

[Option('b', "block-size", Default = 1048576, HelpText = $"Usable with -e or -d, Changes the block size for encryption and decryption. Block size must be divisible by 32. in large files, bigger block size may reduce time and output size.")]
public int BlockSize { get; set; }

Expand Down
71 changes: 57 additions & 14 deletions src/Kryptor.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@

if (opt.Encrypt || opt.Decrypt)
{
KESKeyStore ks = ReadKeystore(opt.KeyStore);
KESKeyStore ks = default;
KESProvider kp = default;

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Debug on windows

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Debug on windows

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Release on windows

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Release on windows

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Debug on ubuntu

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Debug on ubuntu

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Release on ubuntu

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Release on ubuntu

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Debug on macos

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Debug on macos

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Release on macos

Converting null literal or possible null value to non-nullable type.

Check warning on line 19 in src/Kryptor.Tool/Program.cs

View workflow job for this annotation

GitHub Actions / build / Build Release on macos

Converting null literal or possible null value to non-nullable type.

KESProvider kp = new(ks, maxBlockSize: opt.BlockSize, continuous: opt.Continuous);
kp.OnProgress += ShowProgress;
if (opt.Decrypt || !opt.CreateKey)
{
ks = ReadKeystore(opt.KeyStore);
kp = GetProvider(opt, ks);
}

if (opt.Encrypt)
{
foreach (var file in opt.File)
{
if (opt.CreateKey)
{
ks = GenerateKeystore();
kp = GetProvider(opt, ks);
}

Holder.ProcessTime = DateTime.Now;
await Encrypt(file, kp);
}
Expand All @@ -39,15 +49,7 @@
}
else if (opt.Generate)
{
Echo(new Colorize($"Generating keystore with [{opt.KeyStoreSize}] keys", ConsoleColor.Cyan));
KESKeyStore ks = KESKeyStore.Generate(opt.KeyStoreSize);

Echo(new Colorize($"Keystore Fingerprint: [{BitConverter.ToString(ks.Fingerprint)}]", ConsoleColor.Blue));

var fName = !string.IsNullOrEmpty(opt.KeyStore) ? opt.KeyStore : BitConverter.ToString(ks.Fingerprint).Replace("-", "").ToLower() + ".kks";
File.WriteAllText(fName, ks.ToString());

Echo(new Colorize($"Keystore is saved to [{fName}]", ConsoleColor.Green));
GenerateKeystore(opt.KeyStore, opt.KeyStoreSize);
}

return Environment.ExitCode;
Expand Down Expand Up @@ -175,9 +177,29 @@ void IsValid()
{
if (opt.Encrypt || opt.Decrypt)
{
if (string.IsNullOrEmpty(opt.KeyStore) || !opt.File.Any())
if (!opt.CreateKey)
{
Echo(new Colorize("[Error:] You must specify keystore and at least one file.", ConsoleColor.Red));
if (string.IsNullOrEmpty(opt.KeyStore))
{
Echo(new Colorize("[Error:] You must specify keysore file name.", ConsoleColor.Red));
Environment.Exit(2);
}
}
else if (!string.IsNullOrEmpty(opt.KeyStore))
{
if (opt.File != null)
{
opt.File = new string[] { opt.KeyStore }.Concat(opt.File);
}
else
{
opt.File = new string[] { opt.KeyStore };
}
}

if (opt.File == null || !opt.File.Any())
{
Echo(new Colorize("[Error:] You must specify at least one file.", ConsoleColor.Red));
Environment.Exit(2);
}

Expand Down Expand Up @@ -218,3 +240,24 @@ string GetVersionString(Assembly assembly)
var ver = new Version(assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version);
return string.Join('.', ver.Major, ver.Minor, ver.Build);
}

KESKeyStore GenerateKeystore(string name = "", int keystoreSize = 256)
{
Echo(new Colorize($"Generating keystore with [{keystoreSize}] keys", ConsoleColor.Cyan));
KESKeyStore ks = KESKeyStore.Generate(keystoreSize);

Echo(new Colorize($"Keystore Fingerprint: [{BitConverter.ToString(ks.Fingerprint)}]", ConsoleColor.Blue));

var fName = !string.IsNullOrEmpty(name) ? name : BitConverter.ToString(ks.Fingerprint).Replace("-", "").ToLower() + ".kks";
File.WriteAllText(fName, ks.ToString());

Echo(new Colorize($"Keystore is saved to [{fName}]", ConsoleColor.Green));
return ks;
}

KESProvider GetProvider(Arguments opt, KESKeyStore ks)
{
KESProvider kp = new(ks, maxBlockSize: opt.BlockSize, continuous: opt.Continuous);
kp.OnProgress += ShowProgress;
return kp;
}

0 comments on commit e98af74

Please sign in to comment.