-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Security Improvements: Introduction of SecureBigInteger
Resolves: No entry
- Loading branch information
1 parent
eb365a1
commit 0abf23b
Showing
17 changed files
with
3,332 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#if NET6_0_OR_GREATER | ||
namespace SecretSharingDotNet.Helper; | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
|
||
/// <summary> | ||
/// Manages a composite collection of <see cref="IDisposable"/> objects, | ||
/// ensuring all contained disposables are disposed together. | ||
/// </summary> | ||
public sealed class CompositeDisposable : IDisposable | ||
{ | ||
private readonly ConcurrentBag<IDisposable> disposables = new(); | ||
private bool disposed; | ||
|
||
/// <summary> | ||
/// Finalizes an instance of the <see cref="CompositeDisposable"/> class. | ||
/// </summary> | ||
~CompositeDisposable() | ||
{ | ||
this.Dispose(false); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a disposable object to the composite collection of disposables. | ||
/// </summary> | ||
/// <param name="disposable">The disposable object to add.</param> | ||
/// <exception cref="ObjectDisposedException">Thrown when the composite disposable has already been disposed.</exception> | ||
public void Add(IDisposable disposable) | ||
{ | ||
ArgumentNullException.ThrowIfNull(disposable); | ||
if (this.disposed) | ||
{ | ||
throw new ObjectDisposedException(nameof(CompositeDisposable)); | ||
} | ||
|
||
this.disposables.Add(disposable); | ||
} | ||
|
||
/// <summary> | ||
/// Clears and disposes all disposable objects contained within the composite collection. | ||
/// </summary> | ||
public void Clear() | ||
{ | ||
foreach (var disposable in this.disposables) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
|
||
this.disposables.Clear(); | ||
} | ||
|
||
/// <summary> | ||
/// Disposes the composite collection of disposables, releasing all managed resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
this.Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// Disposes the composite collection of disposables, releasing all managed resources. | ||
/// </summary> | ||
/// <param name="disposing">Indicates whether the method call comes from a Dispose method (true) or from a finalizer (false).</param> | ||
private void Dispose(bool disposing) | ||
{ | ||
if (this.disposed) | ||
{ | ||
return; | ||
} | ||
|
||
if (disposing) | ||
{ | ||
foreach (var disposable in this.disposables) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
|
||
this.disposables.Clear(); | ||
this.disposed = true; | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
namespace SecretSharingDotNet.Helper; | ||
|
||
using System.Threading; | ||
|
||
/// <summary> | ||
/// Provides a context for managing a <see cref="CompositeDisposable"/> instance per thread. | ||
/// </summary> | ||
public static class CompositeDisposableContext | ||
{ | ||
/// <summary> | ||
/// Provides a thread-local instance of <see cref="CompositeDisposable"/> that represents | ||
/// the current context for managing disposable resources within the thread. | ||
/// </summary> | ||
public static ThreadLocal<CompositeDisposable> Current { get; } = new ThreadLocal<CompositeDisposable>(); | ||
|
||
/// <summary> | ||
/// Sets the current thread's <see cref="CompositeDisposable"/> instance. | ||
/// </summary> | ||
/// <param name="compositeDisposable">The <see cref="CompositeDisposable"/> instance to set for the current thread.</param> | ||
public static void SetCurrent(CompositeDisposable compositeDisposable) | ||
{ | ||
Current.Value = compositeDisposable; | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
namespace SecretSharingDotNet.Helper; | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
|
||
public sealed class Scope : IDisposable | ||
Check warning on line 8 in src/Helper/Scope.cs GitHub Actions / build
|
||
{ | ||
private readonly ConcurrentDictionary<Type, object> services = new(); | ||
private bool disposed; | ||
|
||
~Scope() | ||
Check warning on line 13 in src/Helper/Scope.cs GitHub Actions / build
|
||
{ | ||
this.Dispose(false); | ||
} | ||
|
||
public T GetScopedSingleton<T>() where T : class, new() | ||
Check warning on line 18 in src/Helper/Scope.cs GitHub Actions / build
|
||
{ | ||
var type = typeof(T); | ||
if (this.services.TryGetValue(type, out object service)) | ||
{ | ||
return service as T; | ||
} | ||
|
||
var newService = new T(); | ||
this.services.TryAdd(type, newService); | ||
return newService; | ||
} | ||
|
||
public void Dispose() | ||
Check warning on line 31 in src/Helper/Scope.cs GitHub Actions / build
|
||
{ | ||
this.Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
private void Dispose(bool disposing) | ||
{ | ||
if (this.disposed) | ||
{ | ||
return; | ||
} | ||
|
||
if (disposing) | ||
{ | ||
foreach (IDisposable service in this.services.Values) | ||
{ | ||
service?.Dispose(); | ||
} | ||
} | ||
|
||
this.disposed = true; | ||
} | ||
} | ||
#endif |
Oops, something went wrong.