Skip to content

Commit

Permalink
Add encrypt to console.
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehjohn committed Jan 13, 2024
1 parent 215efff commit 71b52eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/Security.Console/Infrastructure/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,42 @@
using System;
using System.Linq;
using System.Text;
using Security.Crypto;

namespace Security.Console.Infrastructure
{
public class EntryPoint
public static class EntryPoint
{
public static int Main(string[] args)
{
return Parser.Default.ParseArguments<GenerateKeyOptions, SplitSecretOptions, CombineOptions, ToBase64Options, FromBase64Options>(args)
return Parser.Default.ParseArguments<GenerateKeyOptions, SplitSecretOptions, CombineOptions, ToBase64Options, FromBase64Options, EncryptOptions>(args)
.MapResult(
(GenerateKeyOptions options) => GenerateKey(options),
(SplitSecretOptions options) => SplitSecret(options),
(CombineOptions options) => Combine(options),
(ToBase64Options options) => ToBase64(options),
(FromBase64Options options) => FromBase64(options),
errs => 1);
(EncryptOptions options) => Encrypt(options),
_ => 1);
}

private static int Encrypt(EncryptOptions options)
{
var key = Convert.FromBase64String(options.Key);

var iv = Convert.FromBase64String(options.Iv);

var salt = Convert.FromBase64String(options.Salt);

var data = Convert.FromBase64String(options.Data);

var cipher = new SymmetricCipher();

var encrypted = cipher.Encrypt(data, key, iv, salt);

Output($"\n Encrypted data: {Convert.ToBase64String(encrypted)}");

return 0;
}

private static int GenerateKey(GenerateKeyOptions options)
Expand Down
19 changes: 19 additions & 0 deletions src/Security.Console/Infrastructure/Settings/EncryptOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using CommandLine;

namespace Security.Console.Infrastructure.Settings;

[Verb("Encrypt", HelpText = "Encrypt some data.")]
public class EncryptOptions
{
[Option('k', "Key", Required = true, HelpText = "Base 64 encoded key.")]
public string Key { get; set; }

[Option('i', "IV", Required = true, HelpText = "Base 64 encoded initialisation vector.")]
public string Iv { get; set; }

[Option('s', "Salt", Required = true, HelpText = "Base 64 encoded salt.")]
public string Salt { get; set; }

[Option('d', "Data", Required = true, HelpText = "Base 64 encoded data to be encrypted.")]
public string Data { get; set; }
}

0 comments on commit 71b52eb

Please sign in to comment.