diff --git a/DigitalData.Core.Security/AsymCryptService.cs b/DigitalData.Core.Security/AsymCryptService.cs index 12ea349..cf42c91 100644 --- a/DigitalData.Core.Security/AsymCryptService.cs +++ b/DigitalData.Core.Security/AsymCryptService.cs @@ -1,5 +1,6 @@ using DigitalData.Core.Abstractions.Security; using DigitalData.Core.Security.Config; +using DigitalData.Core.Security.Cryptographer; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; diff --git a/DigitalData.Core.Security/RSACryptographer.cs b/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs similarity index 90% rename from DigitalData.Core.Security/RSACryptographer.cs rename to DigitalData.Core.Security/Cryptographer/RSACryptographer.cs index 1b357bd..a128c62 100644 --- a/DigitalData.Core.Security/RSACryptographer.cs +++ b/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs @@ -1,7 +1,7 @@ using DigitalData.Core.Abstractions.Security; using System.Security.Cryptography; -namespace DigitalData.Core.Security +namespace DigitalData.Core.Security.Cryptographer { public class RSACryptographer : IRSACryptographer { @@ -14,7 +14,7 @@ namespace DigitalData.Core.Security public string? Issuer { get; init; } public string? Audience { get; init; } - + internal RSACryptographer() { } } } \ No newline at end of file diff --git a/DigitalData.Core.Security/RSADecryptor.cs b/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs similarity index 95% rename from DigitalData.Core.Security/RSADecryptor.cs rename to DigitalData.Core.Security/Cryptographer/RSADecryptor.cs index a527fe5..b1bf7b1 100644 --- a/DigitalData.Core.Security/RSADecryptor.cs +++ b/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs @@ -2,7 +2,7 @@ using DigitalData.Core.Security.Extensions; using System.Security.Cryptography; -namespace DigitalData.Core.Security +namespace DigitalData.Core.Security.Cryptographer { public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer { @@ -31,7 +31,7 @@ namespace DigitalData.Core.Security protected override RSA RSA => lazyRSA.Value; - public RSADecryptor() + public RSADecryptor() { _lazyEncryptor = new(() => new RSAEncryptor() { @@ -50,7 +50,7 @@ namespace DigitalData.Core.Security return rsa; }); } - + public byte[] Decrypt(byte[] data) => RSA.Decrypt(data, Padding); public string Decrypt(string data) => RSA.Decrypt(data.Base64ToByte(), Padding).BytesToString(); diff --git a/DigitalData.Core.Security/RSAEncryptor.cs b/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs similarity index 87% rename from DigitalData.Core.Security/RSAEncryptor.cs rename to DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs index 7783902..2445b30 100644 --- a/DigitalData.Core.Security/RSAEncryptor.cs +++ b/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs @@ -1,13 +1,13 @@ using DigitalData.Core.Abstractions.Security; using DigitalData.Core.Security.Extensions; -namespace DigitalData.Core.Security +namespace DigitalData.Core.Security.Cryptographer { public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer { public override required string Pem - { - get => base.Pem; + { + get => base.Pem; init { RSA.ImportFromPem(base.Pem); diff --git a/DigitalData.Core.Security/RSAFactory.cs b/DigitalData.Core.Security/Cryptographer/RSAFactory.cs similarity index 92% rename from DigitalData.Core.Security/RSAFactory.cs rename to DigitalData.Core.Security/Cryptographer/RSAFactory.cs index 1539765..ebdc603 100644 --- a/DigitalData.Core.Security/RSAFactory.cs +++ b/DigitalData.Core.Security/Cryptographer/RSAFactory.cs @@ -3,7 +3,7 @@ using DigitalData.Core.Security.Config; using Microsoft.Extensions.Options; using System.Security.Cryptography; -namespace DigitalData.Core.Security +namespace DigitalData.Core.Security.Cryptographer { public class RSAFactory : IRSAFactory where TRSAFactoryParams : RSAFactoryParams { @@ -12,7 +12,7 @@ namespace DigitalData.Core.Security public static RSAFactory Static => LazyInstance.Value; protected readonly TRSAFactoryParams _params; - + public RSAFactory(IOptions options) => _params = options.Value; public string CreateRSAPrivateKeyPem(int? keySizeInBits = null) @@ -27,13 +27,13 @@ namespace DigitalData.Core.Security { password ??= _params.PbePassword; - var pbeParameters = (pbeEncryptionAlgorithm is null && hashAlgorithmName is null && iterationCount is null) + var pbeParameters = pbeEncryptionAlgorithm is null && hashAlgorithmName is null && iterationCount is null ? new PbeParameters( pbeEncryptionAlgorithm ?? _params.PbeEncryptionAlgorithm, hashAlgorithmName ?? _params.PbeHashAlgorithmName, iterationCount ?? _params.PbeIterationCount) : _params.PbeParameters; - + var encryptedPrivateKey = RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters); var pemChars = PemEncoding.Write(_params.EncryptedPrivateKeyPemLabel, encryptedPrivateKey); @@ -47,7 +47,7 @@ namespace DigitalData.Core.Security (string Value, Version Version)? versionedPassword = null; - if(version is not null) + if (version is not null) { if (version != Secrets.Version) throw new InvalidOperationException($"The provided version {version} does not match the expected version {Secrets.Version}."); diff --git a/DigitalData.Core.Security/DIExtensions.cs b/DigitalData.Core.Security/DIExtensions.cs index c12ee8a..644e770 100644 --- a/DigitalData.Core.Security/DIExtensions.cs +++ b/DigitalData.Core.Security/DIExtensions.cs @@ -1,5 +1,6 @@ using DigitalData.Core.Abstractions.Security; using DigitalData.Core.Security.Config; +using DigitalData.Core.Security.Cryptographer; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; diff --git a/DigitalData.Core.Security/DigitalData.Core.Security.csproj b/DigitalData.Core.Security/DigitalData.Core.Security.csproj index 7c301a2..c198a40 100644 --- a/DigitalData.Core.Security/DigitalData.Core.Security.csproj +++ b/DigitalData.Core.Security/DigitalData.Core.Security.csproj @@ -15,8 +15,4 @@ - - - -