Compare commits
12 Commits
6a92466490
...
f38bad8531
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f38bad8531 | ||
|
|
154478c318 | ||
|
|
155eb563d1 | ||
|
|
4aacc3f650 | ||
|
|
f40c86ed63 | ||
|
|
b32f0df125 | ||
|
|
324a5bdb1e | ||
|
|
e0a6787a87 | ||
|
|
c6a4038eab | ||
|
|
58c8520c08 | ||
|
|
eced1a5afc | ||
|
|
7da93c6719 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -410,3 +410,4 @@ FodyWeavers.xsd
|
|||||||
/DigitalData.Core.ConsoleApp/FooHttpOptions.cs
|
/DigitalData.Core.ConsoleApp/FooHttpOptions.cs
|
||||||
/DigitalData.Core.Tests/obj/
|
/DigitalData.Core.Tests/obj/
|
||||||
/DigitalData.Core.Terminal
|
/DigitalData.Core.Terminal
|
||||||
|
/DigitalData.Core.Tests.API
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
namespace DigitalData.Core.Abstractions.Security
|
namespace DigitalData.Core.Abstractions.Security
|
||||||
{
|
{
|
||||||
public interface IAsymCryptService : IRSAFactory
|
public interface IAsymCryptService : IRSAFactory, IEnumerable<IRSADecryptor>
|
||||||
{
|
{
|
||||||
public IEnumerable<IRSADecryptor> Decryptors { get; }
|
IEnumerable<IRSADecryptor> Decryptors { get; }
|
||||||
|
|
||||||
public IRSADecryptor this[string key] { get; }
|
IRSADecryptor Vault { get; }
|
||||||
|
|
||||||
|
IRSADecryptor this[string key] { get; }
|
||||||
|
|
||||||
|
IEnumerable<IRSAEncryptor> Encryptors { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IAsymCryptService<TParams> : IAsymCryptService, IRSAFactory<TParams> { }
|
public interface IAsymCryptService<TParams> : IAsymCryptService, IRSAFactory<TParams> { }
|
||||||
|
|||||||
@@ -4,19 +4,21 @@ namespace DigitalData.Core.Abstractions.Security
|
|||||||
{
|
{
|
||||||
public interface IRSAFactory
|
public interface IRSAFactory
|
||||||
{
|
{
|
||||||
string CreatePrivateKeyPem(int? keySizeInBits = null);
|
string CreatePrivateKeyPem(int? keySizeInBits = null, bool encrypt = false);
|
||||||
|
|
||||||
public string CreateEncryptedPrivateKeyPem(
|
string CreateEncryptedPrivateKeyPem(
|
||||||
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
|
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
|
||||||
HashAlgorithmName? hashAlgorithmName = null,
|
HashAlgorithmName? hashAlgorithmName = null,
|
||||||
int? iterationCount = null,
|
int? iterationCount = null,
|
||||||
int? keySizeInBits = null,
|
int? keySizeInBits = null,
|
||||||
string? password = null);
|
string? password = null);
|
||||||
|
|
||||||
public string CreateEncryptedPrivateKeyPem(
|
string CreateEncryptedPrivateKeyPem(
|
||||||
PbeParameters pbeParameters,
|
PbeParameters pbeParameters,
|
||||||
int? keySizeInBits = null,
|
int? keySizeInBits = null,
|
||||||
string? password = null);
|
string? password = null);
|
||||||
|
|
||||||
|
IRSADecryptor CreateDecryptor(string pem, string? issuer = null, string? audience = null, bool encrypt = false, RSAEncryptionPadding? padding = null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IRSAFactory<TParams> : IRSAFactory { }
|
public interface IRSAFactory<TParams> : IRSAFactory { }
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace DigitalData.Core.Security.Extensions
|
namespace DigitalData.Core.Extensions
|
||||||
{
|
{
|
||||||
public static class Extensions
|
public static class StringExtensions
|
||||||
{
|
{
|
||||||
public static string ToBase64String(this byte[] bytes) => Convert.ToBase64String(bytes);
|
public static string ToBase64String(this byte[] bytes) => Convert.ToBase64String(bytes);
|
||||||
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\DigitalData.Core.Abstractions\DigitalData.Core.Abstractions.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
using DigitalData.Core.Abstractions.Security;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
|
|
||||||
namespace DigitalData.Core.Security.Extensions
|
|
||||||
{
|
|
||||||
public static class RSAExtensions
|
|
||||||
{
|
|
||||||
public static RSA ToRSA(this string pem)
|
|
||||||
{
|
|
||||||
var rsa = RSA.Create();
|
|
||||||
rsa.ImportFromPem(pem);
|
|
||||||
return rsa;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string CreatePath(string filename, string? directory = null)
|
|
||||||
{
|
|
||||||
directory ??= Environment.CurrentDirectory;
|
|
||||||
|
|
||||||
if (!Directory.Exists(directory))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(directory);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Path.Combine(directory, $"{filename}.pem");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static readonly ConcurrentDictionary<string, SemaphoreSlim> FileLocks = new();
|
|
||||||
|
|
||||||
public static void SavePem(this IRSACryptographer decryptor, string key, string? directory = null)
|
|
||||||
{
|
|
||||||
var filePath = CreatePath(filename: key, directory : directory);
|
|
||||||
var fileLock = FileLocks.GetOrAdd(filePath, _ => new (1, 1));
|
|
||||||
fileLock.Wait();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
File.WriteAllText(filePath, decryptor.Pem);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
fileLock.Release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task SavePemAsync(this IRSACryptographer decryptor, string key, string? directory = null)
|
|
||||||
{
|
|
||||||
var filePath = CreatePath(filename: key, directory: directory);
|
|
||||||
var fileLock = FileLocks.GetOrAdd(filePath, _ => new (1, 1));
|
|
||||||
await fileLock.WaitAsync();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await File.WriteAllTextAsync(filePath, decryptor.Pem);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
fileLock.Release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,12 @@ namespace DigitalData.Core.Security
|
|||||||
public class AsymCryptService<TAsymCryptParams> : RSAFactory<TAsymCryptParams>, IAsymCryptService<TAsymCryptParams>, IRSAFactory<TAsymCryptParams>, IEnumerable<IRSADecryptor>
|
public class AsymCryptService<TAsymCryptParams> : RSAFactory<TAsymCryptParams>, IAsymCryptService<TAsymCryptParams>, IRSAFactory<TAsymCryptParams>, IEnumerable<IRSADecryptor>
|
||||||
where TAsymCryptParams : AsymCryptParams
|
where TAsymCryptParams : AsymCryptParams
|
||||||
{
|
{
|
||||||
public IEnumerable<IRSADecryptor> Decryptors => _params.Decryptors;
|
public IEnumerable<IRSADecryptor> Decryptors { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// It is a separate decryptor for permanently stored encrypted data. It is assigned to the first Default decryptor by default.
|
||||||
|
/// </summary>
|
||||||
|
public IRSADecryptor Vault { get; }
|
||||||
|
|
||||||
public IRSADecryptor this[string key]
|
public IRSADecryptor this[string key]
|
||||||
{
|
{
|
||||||
@@ -29,6 +34,17 @@ namespace DigitalData.Core.Security
|
|||||||
public AsymCryptService(IOptions<TAsymCryptParams> options, ILogger<AsymCryptService<TAsymCryptParams>>? logger = null) : base(options)
|
public AsymCryptService(IOptions<TAsymCryptParams> options, ILogger<AsymCryptService<TAsymCryptParams>>? logger = null) : base(options)
|
||||||
{
|
{
|
||||||
logger?.LogInformation("Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy"));
|
logger?.LogInformation("Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy"));
|
||||||
|
|
||||||
|
if (!_params.Decryptors.Any())
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
"Any decryptor is not found. Ensure that at least one decryptor is configured in the provided parameters. " +
|
||||||
|
"This issue typically arises if the configuration for decryptors is incomplete or missing. " +
|
||||||
|
"Check the 'Decryptors' collection in the configuration and verify that it contains valid entries."
|
||||||
|
);
|
||||||
|
|
||||||
|
Decryptors = _params.Decryptors;
|
||||||
|
|
||||||
|
Vault = _params.Vault ?? Decryptors.First();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerator<IRSADecryptor> GetEnumerator() => Decryptors.GetEnumerator();
|
public IEnumerator<IRSADecryptor> GetEnumerator() => Decryptors.GetEnumerator();
|
||||||
@@ -40,10 +56,8 @@ namespace DigitalData.Core.Security
|
|||||||
get
|
get
|
||||||
{
|
{
|
||||||
foreach (var decryptor in Decryptors)
|
foreach (var decryptor in Decryptors)
|
||||||
{
|
|
||||||
yield return decryptor.Encryptor;
|
yield return decryptor.Encryptor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -36,10 +36,12 @@ namespace DigitalData.Core.Security.Config
|
|||||||
|
|
||||||
public IEnumerable<RSADecryptor> Decryptors { get; init; } = new List<RSADecryptor>();
|
public IEnumerable<RSADecryptor> Decryptors { get; init; } = new List<RSADecryptor>();
|
||||||
|
|
||||||
public override void OnDeserialized()
|
public RSADecryptor? Vault { get; init; }
|
||||||
{
|
|
||||||
base.OnDeserialized();
|
|
||||||
|
|
||||||
|
public AsymCryptParams()
|
||||||
|
{
|
||||||
|
AfterCreate += () =>
|
||||||
|
{
|
||||||
// Create root folder if it does not exist
|
// Create root folder if it does not exist
|
||||||
if (!Directory.Exists(PemDirectory))
|
if (!Directory.Exists(PemDirectory))
|
||||||
Directory.CreateDirectory(PemDirectory);
|
Directory.CreateDirectory(PemDirectory);
|
||||||
@@ -69,9 +71,8 @@ namespace DigitalData.Core.Security.Config
|
|||||||
Task.Run(async () => await File.WriteAllTextAsync(path: path, pem));
|
Task.Run(async () => await File.WriteAllTextAsync(path: path, pem));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
decryptor.Init();
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace DigitalData.Core.Security.Config
|
||||||
|
{
|
||||||
|
public class ParamsConfigureOptions<TParams> : IConfigureOptions<TParams> where TParams : RSAFactoryParams
|
||||||
|
{
|
||||||
|
public void Configure(TParams options) => options.Init();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,28 @@ namespace DigitalData.Core.Security.Config
|
|||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public PbeParameters PbeParameters => _pbeParameters!;
|
public PbeParameters PbeParameters => _pbeParameters!;
|
||||||
|
|
||||||
public virtual void OnDeserialized() => _pbeParameters = new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithmName, PbeIterationCount);
|
/// <summary>
|
||||||
|
/// Provides a thread-safe initialization mechanism using Lazy initialization.
|
||||||
|
/// </summary>
|
||||||
|
private readonly Lazy<bool> _lazyInitializer;
|
||||||
|
|
||||||
|
public bool IsInitialized => _lazyInitializer.IsValueCreated;
|
||||||
|
|
||||||
|
public RSAFactoryParams()
|
||||||
|
{
|
||||||
|
_lazyInitializer = new(() =>
|
||||||
|
{
|
||||||
|
AfterCreate?.Invoke();
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
AfterCreate += () => _pbeParameters = new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithmName, PbeIterationCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected event Action AfterCreate;
|
||||||
|
|
||||||
|
public void Init() => _ = _lazyInitializer.Value;
|
||||||
|
|
||||||
|
public void OnDeserialized() => Init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ using System.Security.Cryptography;
|
|||||||
|
|
||||||
namespace DigitalData.Core.Security.Cryptographer
|
namespace DigitalData.Core.Security.Cryptographer
|
||||||
{
|
{
|
||||||
|
//TODO: Abstract RSA for future updates (using ECC, El Gamal or Lattice-based Cryptography)
|
||||||
public class RSACryptographer : IRSACryptographer
|
public class RSACryptographer : IRSACryptographer
|
||||||
{
|
{
|
||||||
public virtual string Pem { get; init; }
|
public virtual string Pem { get; init; }
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
using DigitalData.Core.Abstractions.Security;
|
using DigitalData.Core.Abstractions.Security;
|
||||||
using DigitalData.Core.Security.Extensions;
|
using DigitalData.Core.Extensions;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace DigitalData.Core.Security.Cryptographer
|
namespace DigitalData.Core.Security.Cryptographer
|
||||||
@@ -8,7 +8,15 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
{
|
{
|
||||||
private string? _pem;
|
private string? _pem;
|
||||||
|
|
||||||
public override string Pem { get => _pem ?? throw PemIsNullException; init => _pem = value; }
|
public override string Pem
|
||||||
|
{
|
||||||
|
get => _pem ?? throw PemIsNullException;
|
||||||
|
init
|
||||||
|
{
|
||||||
|
_pem = value;
|
||||||
|
Init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool IsPemNull => _pem is null;
|
public bool IsPemNull => _pem is null;
|
||||||
|
|
||||||
@@ -31,11 +39,15 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
|
|
||||||
public string Decrypt(string data) => RSA.Decrypt(data.Base64ToByte(), Padding).BytesToString();
|
public string Decrypt(string data) => RSA.Decrypt(data.Base64ToByte(), Padding).BytesToString();
|
||||||
|
|
||||||
internal void SetPem(string pem) => _pem = pem;
|
internal void SetPem(string pem)
|
||||||
|
|
||||||
public void Init()
|
|
||||||
{
|
{
|
||||||
if (_pem is null)
|
_pem = pem;
|
||||||
|
Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Init()
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(_pem))
|
||||||
throw PemIsNullException;
|
throw PemIsNullException;
|
||||||
|
|
||||||
if (IsEncrypted)
|
if (IsEncrypted)
|
||||||
@@ -44,6 +56,6 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
RSA.ImportFromPem(Pem);
|
RSA.ImportFromPem(Pem);
|
||||||
}
|
}
|
||||||
|
|
||||||
private InvalidOperationException PemIsNullException => new($"Pem is not initialized. Please ensure that the PEM is set or properly loaded from the file. Issuer: {Issuer}, Audience: {Audience}.");
|
private InvalidOperationException PemIsNullException => new($"Pem is null or empty. Issuer: {Issuer}, Audience: {Audience}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
using DigitalData.Core.Abstractions.Security;
|
using DigitalData.Core.Abstractions.Security;
|
||||||
using DigitalData.Core.Security.Extensions;
|
using DigitalData.Core.Extensions;
|
||||||
|
|
||||||
namespace DigitalData.Core.Security.Cryptographer
|
namespace DigitalData.Core.Security.Cryptographer
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,10 +9,15 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
{
|
{
|
||||||
protected readonly TRSAFactoryParams _params;
|
protected readonly TRSAFactoryParams _params;
|
||||||
|
|
||||||
public RSAFactory(IOptions<TRSAFactoryParams> options) => _params = options.Value;
|
public RSAFactory(IOptions<TRSAFactoryParams> options)
|
||||||
|
{
|
||||||
|
options.Value.Init();
|
||||||
|
_params = options.Value;
|
||||||
|
}
|
||||||
|
|
||||||
public string CreatePrivateKeyPem(int? keySizeInBits = null)
|
public string CreatePrivateKeyPem(int? keySizeInBits = null, bool encrypt = false) => encrypt
|
||||||
=> RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportRSAPrivateKeyPem();
|
? CreateEncryptedPrivateKeyPem(keySizeInBits: keySizeInBits)
|
||||||
|
: RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportRSAPrivateKeyPem();
|
||||||
|
|
||||||
public string CreateEncryptedPrivateKeyPem(
|
public string CreateEncryptedPrivateKeyPem(
|
||||||
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
|
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
|
||||||
@@ -50,5 +55,14 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
|
|
||||||
return new string(pemChars);
|
return new string(pemChars);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IRSADecryptor CreateDecryptor(string pem, string? issuer = null, string? audience = null, bool encrypt = false, RSAEncryptionPadding? padding = null) => new RSADecryptor()
|
||||||
|
{
|
||||||
|
Pem = pem,
|
||||||
|
Issuer = issuer ?? string.Empty,
|
||||||
|
Audience = audience ?? string.Empty,
|
||||||
|
IsEncrypted = encrypt,
|
||||||
|
Padding = padding ?? RSAEncryptionPadding.OaepSHA256
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,10 +21,13 @@ namespace DigitalData.Core.Security
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static IServiceCollection AddParamsConfigureOptions<TParams>(this IServiceCollection services) where TParams : RSAFactoryParams
|
||||||
|
=> services.AddSingleton<IConfigureOptions<TParams>, ParamsConfigureOptions<TParams>>();
|
||||||
|
|
||||||
private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams
|
private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams
|
||||||
=> setAsDefault
|
=> setAsDefault
|
||||||
? services.AddScoped<IAsymCryptService, AsymCryptService<TAsymCryptParams>>()
|
? services.AddParamsConfigureOptions<TAsymCryptParams>().AddSingleton<IAsymCryptService, AsymCryptService<TAsymCryptParams>>()
|
||||||
: services.AddScoped<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>();
|
: services.AddParamsConfigureOptions<TAsymCryptParams>().AddSingleton<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers a custom asym crypt service with specified parameters from the given configuration section.
|
/// Registers a custom asym crypt service with specified parameters from the given configuration section.
|
||||||
@@ -34,9 +37,19 @@ namespace DigitalData.Core.Security
|
|||||||
/// <param name="section"></param>
|
/// <param name="section"></param>
|
||||||
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
|
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
|
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams => services
|
||||||
where TAsymCryptParams : AsymCryptParams
|
.Configure<TAsymCryptParams>(section)
|
||||||
=> services.Configure<TAsymCryptParams>(section).AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault);
|
.AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers a custom asym crypt service with default parameters from the given configuration section.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <param name="section"></param>
|
||||||
|
/// <param name="setAsDefault"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
|
||||||
|
=> services.Configure<AsymCryptParams>(section).AddAsymCryptService<AsymCryptParams>(setAsDefault: setAsDefault);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers an asym crypt service with the specified parameters from the given instance. Optionally, sets it as the default factory.
|
/// Registers an asym crypt service with the specified parameters from the given instance. Optionally, sets it as the default factory.
|
||||||
@@ -46,9 +59,9 @@ namespace DigitalData.Core.Security
|
|||||||
/// <param name="param"></param>
|
/// <param name="param"></param>
|
||||||
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
|
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, TAsymCryptParams param, bool setAsDefault = false)
|
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, TAsymCryptParams param, bool setAsDefault = false) where TAsymCryptParams : AsymCryptParams => services
|
||||||
where TAsymCryptParams : AsymCryptParams
|
.AddSingleton(Options.Create(param))
|
||||||
=> services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault);
|
.AddAsymCryptService<TAsymCryptParams>(setAsDefault: setAsDefault);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers default asym crypt service with the specified parameters from the given instance.
|
/// Registers default asym crypt service with the specified parameters from the given instance.
|
||||||
@@ -56,7 +69,8 @@ namespace DigitalData.Core.Security
|
|||||||
/// <param name="services"></param>
|
/// <param name="services"></param>
|
||||||
/// <param name="param"></param>
|
/// <param name="param"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams param) => services.AddAsymCryptService(param: param, setAsDefault: true);
|
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams param) => services
|
||||||
|
.AddAsymCryptService(param: param, setAsDefault: true);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers default RSA Factory instance with default params
|
/// Registers default RSA Factory instance with default params
|
||||||
@@ -64,8 +78,9 @@ namespace DigitalData.Core.Security
|
|||||||
/// <param name="services"></param>
|
/// <param name="services"></param>
|
||||||
/// <param name="factoryParams"></param>
|
/// <param name="factoryParams"></param>
|
||||||
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
|
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
|
||||||
public static IServiceCollection AddRSAFactory(this IServiceCollection services, RSAFactoryParams? factoryParams = null)
|
public static IServiceCollection AddRSAFactory(this IServiceCollection services, RSAFactoryParams? factoryParams = null) => services
|
||||||
=> services.AddScoped<IRSAFactory>(_ => new RSAFactory<RSAFactoryParams>(Options.Create(factoryParams ?? new())));
|
.AddParamsConfigureOptions<RSAFactoryParams>()
|
||||||
|
.AddScoped<IRSAFactory>(_ => new RSAFactory<RSAFactoryParams>(Options.Create(factoryParams ?? new())));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers a custom RSA Factory with specified parameters from the given configuration section.
|
/// Registers a custom RSA Factory with specified parameters from the given configuration section.
|
||||||
@@ -78,7 +93,7 @@ namespace DigitalData.Core.Security
|
|||||||
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
|
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
|
||||||
where TRSAFactoryParams : RSAFactoryParams
|
where TRSAFactoryParams : RSAFactoryParams
|
||||||
{
|
{
|
||||||
services.Configure<TRSAFactoryParams>(section);
|
services.AddParamsConfigureOptions<TRSAFactoryParams>().Configure<TRSAFactoryParams>(section);
|
||||||
return setAsDefault
|
return setAsDefault
|
||||||
? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
|
? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
|
||||||
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
|
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
|
||||||
@@ -97,8 +112,8 @@ namespace DigitalData.Core.Security
|
|||||||
{
|
{
|
||||||
services.AddSingleton(Options.Create(rsaParams));
|
services.AddSingleton(Options.Create(rsaParams));
|
||||||
return setAsDefault
|
return setAsDefault
|
||||||
? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
|
? services.AddParamsConfigureOptions<TRSAFactoryParams>().AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
|
||||||
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
|
: services.AddParamsConfigureOptions<TRSAFactoryParams>().AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\DigitalData.Core.Abstractions\DigitalData.Core.Abstractions.csproj" />
|
<ProjectReference Include="..\DigitalData.Core.Abstractions\DigitalData.Core.Abstractions.csproj" />
|
||||||
<ProjectReference Include="..\DigitalData.Core.Security.Extensions\DigitalData.Core.Security.Extensions.csproj" />
|
<ProjectReference Include="..\DigitalData.Core.Extensions\DigitalData.Core.Extensions.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Legacy.Cli
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Security", "DigitalData.Core.Security\DigitalData.Core.Security.csproj", "{47D80C65-74A2-4EB8-96A5-D571A9108FB3}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Security", "DigitalData.Core.Security\DigitalData.Core.Security.csproj", "{47D80C65-74A2-4EB8-96A5-D571A9108FB3}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Security.Extensions", "DigitalData.Core.Security.Extensions\DigitalData.Core.Security.Extensions.csproj", "{D740182D-82DA-480A-9F87-BFB4A8620A00}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Terminal", "DigitalData.Core.Terminal\DigitalData.Core.Terminal.csproj", "{0FA93730-8084-4907-B172-87D610323796}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Terminal", "DigitalData.Core.Terminal\DigitalData.Core.Terminal.csproj", "{0FA93730-8084-4907-B172-87D610323796}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Extensions", "DigitalData.Core.Extensions\DigitalData.Core.Extensions.csproj", "{FC6AD1C4-5D7C-4B50-9330-B7A0E52B24B8}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -72,14 +72,14 @@ Global
|
|||||||
{47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Release|Any CPU.Build.0 = Release|Any CPU
|
{47D80C65-74A2-4EB8-96A5-D571A9108FB3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{D740182D-82DA-480A-9F87-BFB4A8620A00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{D740182D-82DA-480A-9F87-BFB4A8620A00}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{D740182D-82DA-480A-9F87-BFB4A8620A00}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{D740182D-82DA-480A-9F87-BFB4A8620A00}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
{0FA93730-8084-4907-B172-87D610323796}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{0FA93730-8084-4907-B172-87D610323796}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{0FA93730-8084-4907-B172-87D610323796}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{0FA93730-8084-4907-B172-87D610323796}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{0FA93730-8084-4907-B172-87D610323796}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{0FA93730-8084-4907-B172-87D610323796}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{0FA93730-8084-4907-B172-87D610323796}.Release|Any CPU.Build.0 = Release|Any CPU
|
{0FA93730-8084-4907-B172-87D610323796}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{FC6AD1C4-5D7C-4B50-9330-B7A0E52B24B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{FC6AD1C4-5D7C-4B50-9330-B7A0E52B24B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{FC6AD1C4-5D7C-4B50-9330-B7A0E52B24B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{FC6AD1C4-5D7C-4B50-9330-B7A0E52B24B8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
Reference in New Issue
Block a user