rektor(RSA): Umbenennung von dir in cryptographer und Verschiebung der zugehörigen Klassen

This commit is contained in:
Developer 02 2024-12-05 10:03:39 +01:00
parent 6e4942c885
commit c38f7dcf72
7 changed files with 15 additions and 17 deletions

View File

@ -1,5 +1,6 @@
using DigitalData.Core.Abstractions.Security; using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config; using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.Cryptographer;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;

View File

@ -1,7 +1,7 @@
using DigitalData.Core.Abstractions.Security; using DigitalData.Core.Abstractions.Security;
using System.Security.Cryptography; using System.Security.Cryptography;
namespace DigitalData.Core.Security namespace DigitalData.Core.Security.Cryptographer
{ {
public class RSACryptographer : IRSACryptographer public class RSACryptographer : IRSACryptographer
{ {
@ -14,7 +14,7 @@ namespace DigitalData.Core.Security
public string? Issuer { get; init; } public string? Issuer { get; init; }
public string? Audience { get; init; } public string? Audience { get; init; }
internal RSACryptographer() { } internal RSACryptographer() { }
} }
} }

View File

@ -2,7 +2,7 @@
using DigitalData.Core.Security.Extensions; using DigitalData.Core.Security.Extensions;
using System.Security.Cryptography; using System.Security.Cryptography;
namespace DigitalData.Core.Security namespace DigitalData.Core.Security.Cryptographer
{ {
public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer
{ {
@ -31,7 +31,7 @@ namespace DigitalData.Core.Security
protected override RSA RSA => lazyRSA.Value; protected override RSA RSA => lazyRSA.Value;
public RSADecryptor() public RSADecryptor()
{ {
_lazyEncryptor = new(() => new RSAEncryptor() _lazyEncryptor = new(() => new RSAEncryptor()
{ {
@ -50,7 +50,7 @@ namespace DigitalData.Core.Security
return rsa; return rsa;
}); });
} }
public byte[] Decrypt(byte[] data) => RSA.Decrypt(data, Padding); public byte[] Decrypt(byte[] data) => RSA.Decrypt(data, Padding);
public string Decrypt(string data) => RSA.Decrypt(data.Base64ToByte(), Padding).BytesToString(); public string Decrypt(string data) => RSA.Decrypt(data.Base64ToByte(), Padding).BytesToString();

View File

@ -1,13 +1,13 @@
using DigitalData.Core.Abstractions.Security; using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Extensions; using DigitalData.Core.Security.Extensions;
namespace DigitalData.Core.Security namespace DigitalData.Core.Security.Cryptographer
{ {
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
{ {
public override required string Pem public override required string Pem
{ {
get => base.Pem; get => base.Pem;
init init
{ {
RSA.ImportFromPem(base.Pem); RSA.ImportFromPem(base.Pem);

View File

@ -3,7 +3,7 @@ using DigitalData.Core.Security.Config;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Security.Cryptography; using System.Security.Cryptography;
namespace DigitalData.Core.Security namespace DigitalData.Core.Security.Cryptographer
{ {
public class RSAFactory<TRSAFactoryParams> : IRSAFactory<TRSAFactoryParams> where TRSAFactoryParams : RSAFactoryParams public class RSAFactory<TRSAFactoryParams> : IRSAFactory<TRSAFactoryParams> where TRSAFactoryParams : RSAFactoryParams
{ {
@ -12,7 +12,7 @@ namespace DigitalData.Core.Security
public static RSAFactory<RSAFactoryParams> Static => LazyInstance.Value; public static RSAFactory<RSAFactoryParams> Static => LazyInstance.Value;
protected readonly TRSAFactoryParams _params; protected readonly TRSAFactoryParams _params;
public RSAFactory(IOptions<TRSAFactoryParams> options) => _params = options.Value; public RSAFactory(IOptions<TRSAFactoryParams> options) => _params = options.Value;
public string CreateRSAPrivateKeyPem(int? keySizeInBits = null) public string CreateRSAPrivateKeyPem(int? keySizeInBits = null)
@ -27,13 +27,13 @@ namespace DigitalData.Core.Security
{ {
password ??= _params.PbePassword; 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( ? new PbeParameters(
pbeEncryptionAlgorithm ?? _params.PbeEncryptionAlgorithm, pbeEncryptionAlgorithm ?? _params.PbeEncryptionAlgorithm,
hashAlgorithmName ?? _params.PbeHashAlgorithmName, hashAlgorithmName ?? _params.PbeHashAlgorithmName,
iterationCount ?? _params.PbeIterationCount) iterationCount ?? _params.PbeIterationCount)
: _params.PbeParameters; : _params.PbeParameters;
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters); var encryptedPrivateKey = RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
var pemChars = PemEncoding.Write(_params.EncryptedPrivateKeyPemLabel, encryptedPrivateKey); var pemChars = PemEncoding.Write(_params.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
@ -47,7 +47,7 @@ namespace DigitalData.Core.Security
(string Value, Version Version)? versionedPassword = null; (string Value, Version Version)? versionedPassword = null;
if(version is not null) if (version is not null)
{ {
if (version != Secrets.Version) if (version != Secrets.Version)
throw new InvalidOperationException($"The provided version {version} does not match the expected version {Secrets.Version}."); throw new InvalidOperationException($"The provided version {version} does not match the expected version {Secrets.Version}.");

View File

@ -1,5 +1,6 @@
using DigitalData.Core.Abstractions.Security; using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Config; using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.Cryptographer;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;

View File

@ -15,8 +15,4 @@
<ProjectReference Include="..\DigitalData.Core.Security.Extensions\DigitalData.Core.Security.Extensions.csproj" /> <ProjectReference Include="..\DigitalData.Core.Security.Extensions\DigitalData.Core.Security.Extensions.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="RSA\" />
</ItemGroup>
</Project> </Project>