Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use file-scoped namespaces #7

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CSharp/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ csharp_style_prefer_readonly_struct_member = true
# Code-block preferences
csharp_prefer_braces = true
csharp_prefer_simple_using_statement = true
csharp_style_namespace_declarations = block_scoped
csharp_style_namespace_declarations = file_scoped:warning
csharp_style_prefer_method_group_conversion = true
csharp_style_prefer_primary_constructors = true
csharp_style_prefer_top_level_statements = true
Expand Down
79 changes: 39 additions & 40 deletions src/CSharp/Cask.Benchmarks/CompareHashedSignatureBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,62 @@

using System.Security.Cryptography;

namespace CommonAnnotatedSecurityKeys.Benchmarks
namespace CommonAnnotatedSecurityKeys.Benchmarks;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespace

If it were done when 'tis done, then 'twere well it were done quickly


public class CompareHashedSignatureBenchmarks
{
public class CompareHashedSignatureBenchmarks
private const int iterations = 10000;
private const string TestProviderSignature = "TEST";
private static readonly byte[] TestProviderSignatureBytes = Convert.FromBase64String(TestProviderSignature);

[Benchmark]
public void UseCompareHash()
{
private const int iterations = 10000;
private const string TestProviderSignature = "TEST";
private static readonly byte [] TestProviderSignatureBytes = Convert.FromBase64String(TestProviderSignature);
string key = Cask.Instance.GenerateKey(TestProviderSignature, "99");
byte[] keyBytes = Convert.FromBase64String(key.FromUrlSafe());
string hash = Cask.Instance.GenerateHash(keyBytes, keyBytes, 32);
byte[] hashBytes = Convert.FromBase64String(hash.FromUrlSafe());

[Benchmark]
public void UseCompareHash()
for (int i = 0; i < iterations; i++)
{
string key = Cask.Instance.GenerateKey(TestProviderSignature, "99");
byte[] keyBytes = Convert.FromBase64String(key.FromUrlSafe());
string hash = Cask.Instance.GenerateHash(keyBytes, keyBytes, 32);
byte[] hashBytes = Convert.FromBase64String(hash.FromUrlSafe());

for (int i = 0; i < iterations; i++)
if (!Cask.Instance.CompareHash(hashBytes, keyBytes, keyBytes))
{
if (!Cask.Instance.CompareHash(hashBytes, keyBytes, keyBytes))
{
throw new InvalidOperationException();
}
throw new InvalidOperationException();
}
}
}

[Benchmark]
public void UseHmacSha256()
[Benchmark]
public void UseHmacSha256()
{
string key = Cask.Instance.GenerateKey(TestProviderSignature, "99");
byte[] keyBytes = Convert.FromBase64String(key.FromUrlSafe());
string hash = Cask.Instance.GenerateHash(TestProviderSignatureBytes, keyBytes, 32);
var hmac = new HMACSHA256(keyBytes);
byte[] hashBytes = hmac.ComputeHash(TestProviderSignatureBytes);

for (int i = 0; i < iterations; i++)
{
string key = Cask.Instance.GenerateKey(TestProviderSignature, "99");
byte[] keyBytes = Convert.FromBase64String(key.FromUrlSafe());
string hash = Cask.Instance.GenerateHash(TestProviderSignatureBytes, keyBytes, 32);
var hmac = new HMACSHA256(keyBytes);
byte[] hashBytes = hmac.ComputeHash(TestProviderSignatureBytes);
hmac = new HMACSHA256(keyBytes);
byte[] computedHashBytes = hmac.ComputeHash(TestProviderSignatureBytes);

for (int i = 0; i < iterations; i++)
for (int j = 0; j < hashBytes.Length; j++)
{
hmac = new HMACSHA256(keyBytes);
byte[] computedHashBytes = hmac.ComputeHash(TestProviderSignatureBytes);

for (int j = 0; j < hashBytes.Length; j++)
if (hashBytes[j] != computedHashBytes[j])
{
if (hashBytes[j] != computedHashBytes[j])
{
throw new InvalidOperationException();
}
throw new InvalidOperationException();
}
}

computedHashBytes = hmac.ComputeHash(TestProviderSignatureBytes);
computedHashBytes = hmac.ComputeHash(TestProviderSignatureBytes);

for (int j = 0; j < hashBytes.Length; j++)
for (int j = 0; j < hashBytes.Length; j++)
{
if (hashBytes[j] != computedHashBytes[j])
{
if (hashBytes[j] != computedHashBytes[j])
{
throw new InvalidOperationException();
}
throw new InvalidOperationException();
}
}
}
}
}
}
39 changes: 19 additions & 20 deletions src/CSharp/Cask.Benchmarks/IsCaskBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@

using BenchmarkDotNet.Attributes;

namespace CommonAnnotatedSecurityKeys.Benchmarks
namespace CommonAnnotatedSecurityKeys.Benchmarks;

public class IsCaskBenchmarks
{
public class IsCaskBenchmarks
{
private const int iterations = 100000;
private const string TestProviderSignature = "TEST";
private static readonly string key = Cask.Instance.GenerateKey(TestProviderSignature, "99");
private static readonly byte[] keyBytes = Convert.FromBase64String(key);
private const int iterations = 100000;
private const string TestProviderSignature = "TEST";
private static readonly string key = Cask.Instance.GenerateKey(TestProviderSignature, "99");
private static readonly byte[] keyBytes = Convert.FromBase64String(key);

[Benchmark]
public void UseIsCaskString()
[Benchmark]
public void UseIsCaskString()
{
for (int i = 0; i < iterations; i++)
{
for (int i = 0; i < iterations; i++)
{
Cask.Instance.IsCask(key);
}
Cask.Instance.IsCask(key);
}
}

[Benchmark]
public void UseIsCaskBytes()
[Benchmark]
public void UseIsCaskBytes()
{
for (int i = 0; i < iterations; i++)
{
for (int i = 0; i < iterations; i++)
{
Cask.Instance.IsCask(keyBytes);
}
Cask.Instance.IsCask(keyBytes);
}
}
}
}
2 changes: 1 addition & 1 deletion src/CSharp/Cask.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
* dotnet run -c Release --framework net8.0 Cask.Benchmarks.csproj
*/
//Summary summary = BenchmarkRunner.Run<IsCaskBenchmarks>();
Summary summary = BenchmarkRunner.Run<CompareHashedSignatureBenchmarks>();
Summary summary = BenchmarkRunner.Run<CompareHashedSignatureBenchmarks>();
19 changes: 9 additions & 10 deletions src/CSharp/Cask.Cli/CloudInstance.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Cask.Cli
namespace Cask.Cli;

public enum CloudInstance
{
public enum CloudInstance
{
None,
NoCloudSpecified = None,
Other,
Public,
Preproduction,
}
}
None,
NoCloudSpecified = None,
Other,
Public,
Preproduction,
}
79 changes: 39 additions & 40 deletions src/CSharp/Cask.Cli/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,58 @@

using System.Text;

namespace CommonAnnotatedSecurityKeys.Cli
{
public class GenerateCommand
{
private readonly string MicrosoftTenantId = new Guid("782ef2bb-3056-4438-946d-395022a4a19f").ToString();
namespace CommonAnnotatedSecurityKeys.Cli;

internal int Run(GenerateOptions options)
{
var cask = new Cask();
byte[] derivationInput = Encoding.UTF8.GetBytes(nameof(derivationInput));
public class GenerateCommand
{
private readonly string MicrosoftTenantId = new Guid("782ef2bb-3056-4438-946d-395022a4a19f").ToString();

string cloudText = "AC";
string region = EncodeForIdentifiableKey("westus");
string tenant = EncodeForIdentifiableKey(MicrosoftTenantId);
string providerReserved = "AAAA";
byte[] reserved = Convert.FromBase64String($"{cloudText}{region}{tenant}{providerReserved}");
internal int Run(GenerateOptions options)
{
var cask = new Cask();
byte[] derivationInput = Encoding.UTF8.GetBytes(nameof(derivationInput));

byte[] providerSignature = Convert.FromBase64String(options.FixedSignature.ToUrlSafe());
string cloudText = "AC";
string region = EncodeForIdentifiableKey("westus");
string tenant = EncodeForIdentifiableKey(MicrosoftTenantId);
string providerReserved = "AAAA";
byte[] reserved = Convert.FromBase64String($"{cloudText}{region}{tenant}{providerReserved}");

for (int i = 0; i < options.Count; i++)
{
byte[] providerSignature = Convert.FromBase64String(options.FixedSignature.ToUrlSafe());

byte[] keyBytes = cask.GenerateKeyBytes(providerSignature,
"99",
reserved,
secretEntropyInBytes: options.SecretEntropyInBytes,
testChar: default);
for (int i = 0; i < options.Count; i++)
{

byte[] keyBytes = cask.GenerateKeyBytes(providerSignature,
"99",
reserved,
secretEntropyInBytes: options.SecretEntropyInBytes,
testChar: default);

string key = Convert.ToBase64String(keyBytes).ToUrlSafe();

string validity = cask.IsCask(key) ? "Valid Key " : "INVALID KEY ";
Console.WriteLine($"{validity}: {key}");
string key = Convert.ToBase64String(keyBytes).ToUrlSafe();

keyBytes = Convert.FromBase64String(key.FromUrlSafe());
string hash = cask.GenerateHash(derivationInput, keyBytes, options.SecretEntropyInBytes);
string validity = cask.IsCask(key) ? "Valid Key " : "INVALID KEY ";
Console.WriteLine($"{validity}: {key}");

validity = cask.IsCask(hash) ? "Valid Hash " : "INVALID HASH";
Console.WriteLine($"{validity}: {hash}");
}
keyBytes = Convert.FromBase64String(key.FromUrlSafe());
string hash = cask.GenerateHash(derivationInput, keyBytes, options.SecretEntropyInBytes);

return 0;
validity = cask.IsCask(hash) ? "Valid Hash " : "INVALID HASH";
Console.WriteLine($"{validity}: {hash}");
}

public static string EncodeForIdentifiableKey(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return "AAAAA";
}
return 0;
}

byte[] hashed = CaskUtilityApi.Sha256.ComputeHash(Encoding.UTF8.GetBytes(text));
return Convert.ToBase64String(hashed).ToUrlSafe().Substring(0, 5);
public static string EncodeForIdentifiableKey(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return "AAAAA";
}

byte[] hashed = CaskUtilityApi.Sha256.ComputeHash(Encoding.UTF8.GetBytes(text));
return Convert.ToBase64String(hashed).ToUrlSafe().Substring(0, 5);
}
}
}
57 changes: 28 additions & 29 deletions src/CSharp/Cask.Cli/GenerateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@
#nullable disable
using CommandLine;

namespace CommonAnnotatedSecurityKeys.Cli
namespace CommonAnnotatedSecurityKeys.Cli;

[Verb("generate", HelpText = "Generate one or more common annotated security keys.")]
public class GenerateOptions
{
[Verb("generate", HelpText = "Generate one or more common annotated security keys.")]
public class GenerateOptions
{
[Option(
"test",
Required = false,
HelpText = "Generate all test keys.")]
public bool Test { get; set; }
[Option(
"test",
Required = false,
HelpText = "Generate all test keys.")]
public bool Test { get; set; }

[Option(
"signature",
Required = false,
Default = "TEST",
HelpText = "A fixed signature to inject into the generated key).")]
public string FixedSignature { get; set; }
[Option(
"signature",
Required = false,
Default = "TEST",
HelpText = "A fixed signature to inject into the generated key).")]
public string FixedSignature { get; set; }

[Option(
"length",
Required = false,
Default = 32,
HelpText = "The length of the randomized component in bytes. Must be at least 16.")]
public int SecretEntropyInBytes { get; set; }
[Option(
"length",
Required = false,
Default = 32,
HelpText = "The length of the randomized component in bytes. Must be at least 16.")]
public int SecretEntropyInBytes { get; set; }

[Option(
"count",
Required = false,
Default = (uint)1,
HelpText = "The count of keys to generate.")]
public uint Count { get; set; }
}
}
[Option(
"count",
Required = false,
Default = (uint)1,
HelpText = "The count of keys to generate.")]
public uint Count { get; set; }
}
Loading