From fe70f7e29d8c3e25995838195742a4aa53100d63 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Wed, 3 Jan 2024 00:48:45 -0800 Subject: [PATCH] Add support for ISecurityProvider --- .../EasyMicroservices.Security.Tests.csproj | 5 ++-- .../BaseEncryptionProviderTest.cs | 2 +- .../HashProviders/BaseHashProviderTest.cs | 2 ++ .../BaseSignatureProviderTest.cs | 5 +++- .../EasyMicroservices.Security.csproj | 4 +-- .../ISecurityProvider.cs | 19 ++++++++++++++ .../Interfaces/IEncryptionProvider.cs | 2 +- .../Interfaces/IHashProvider.cs | 2 +- .../Interfaces/ISignatureProvider.cs | 2 +- .../Providers/BaseSecurityProvider.cs | 25 +++++++++++++++++-- .../BaseEncryptionProvider.cs | 11 ++++++++ .../HashProviders/BaseHashProvider.cs | 12 ++++++++- .../HashProviders/MD5HashProvider.cs | 10 ++++++++ .../BaseSignatureProvider.cs | 10 ++++++++ 14 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 src/CSharp/EasyMicroservices.Security/ISecurityProvider.cs diff --git a/src/CSharp/EasyMicroservices.Security.Tests/EasyMicroservices.Security.Tests.csproj b/src/CSharp/EasyMicroservices.Security.Tests/EasyMicroservices.Security.Tests.csproj index 2547a39..bd4bdd0 100644 --- a/src/CSharp/EasyMicroservices.Security.Tests/EasyMicroservices.Security.Tests.csproj +++ b/src/CSharp/EasyMicroservices.Security.Tests/EasyMicroservices.Security.Tests.csproj @@ -7,10 +7,9 @@ - - + + - diff --git a/src/CSharp/EasyMicroservices.Security.Tests/Providers/EncriptionProviders/BaseEncryptionProviderTest.cs b/src/CSharp/EasyMicroservices.Security.Tests/Providers/EncriptionProviders/BaseEncryptionProviderTest.cs index 97902be..62d00d6 100644 --- a/src/CSharp/EasyMicroservices.Security.Tests/Providers/EncriptionProviders/BaseEncryptionProviderTest.cs +++ b/src/CSharp/EasyMicroservices.Security.Tests/Providers/EncriptionProviders/BaseEncryptionProviderTest.cs @@ -51,7 +51,7 @@ public virtual void Test_Symmetric_WithDifferentKey(string originalDataString) var arrayByte = encryptedData; // Assert Assert.ThrowsAny(() => _anotherProvider.Decrypt(arrayByte)); - + Assert.True(encryptedData.SequenceEqual(_provider.Compute(data))); } [Theory] diff --git a/src/CSharp/EasyMicroservices.Security.Tests/Providers/HashProviders/BaseHashProviderTest.cs b/src/CSharp/EasyMicroservices.Security.Tests/Providers/HashProviders/BaseHashProviderTest.cs index 5e79972..364c7e5 100644 --- a/src/CSharp/EasyMicroservices.Security.Tests/Providers/HashProviders/BaseHashProviderTest.cs +++ b/src/CSharp/EasyMicroservices.Security.Tests/Providers/HashProviders/BaseHashProviderTest.cs @@ -33,6 +33,7 @@ public virtual void ComputeHash_ReturnsExpectedHash(string data) Assert.Equal(hash.Length, hash.Length); Assert.Equal(BitConverter.ToString(realHash).Replace("-", "").ToLower(), BitConverter.ToString(hash).Replace("-", "").ToLower()); Assert.True(hash.SequenceEqual(realHash)); + Assert.True(hash.SequenceEqual(_provider.Compute(bytes))); } [Theory] @@ -51,6 +52,7 @@ public virtual async Task ComputeStreamHash(string data) Assert.Equal(hash.Length, hash.Length); Assert.Equal(BitConverter.ToString(realHash).Replace("-", "").ToLower(), BitConverter.ToString(hash).Replace("-", "").ToLower()); Assert.True(hash.SequenceEqual(realHash)); + Assert.True(hash.SequenceEqual(_provider.Compute(bytes))); } } } diff --git a/src/CSharp/EasyMicroservices.Security.Tests/Providers/SignatureProviders/BaseSignatureProviderTest.cs b/src/CSharp/EasyMicroservices.Security.Tests/Providers/SignatureProviders/BaseSignatureProviderTest.cs index ae0b09e..d8ceeb8 100644 --- a/src/CSharp/EasyMicroservices.Security.Tests/Providers/SignatureProviders/BaseSignatureProviderTest.cs +++ b/src/CSharp/EasyMicroservices.Security.Tests/Providers/SignatureProviders/BaseSignatureProviderTest.cs @@ -1,5 +1,6 @@ using EasyMicroservices.Security.Interfaces; using System.IO; +using System.Linq; using System.Text; using System.Threading.Tasks; using Xunit; @@ -29,6 +30,7 @@ public virtual void SignatureAlgorithmSignAndValidateDataSuccess(string dataStri // assert Assert.True(isValid); + Assert.True(signature.SequenceEqual(_provider.Compute(data))); } [Theory] @@ -46,6 +48,7 @@ public virtual async Task SignatureAlgorithmSignAndValidateDataSuccessStream(str // assert Assert.True(isValid); + Assert.True(signature.SequenceEqual(_provider.Compute(data))); } [Theory] @@ -62,7 +65,7 @@ public virtual void SignatureAlgorithmSignAndValidateDataFalse(string dataString // assert Assert.False(isValid); + Assert.True(signature.SequenceEqual(_provider.Compute(data))); } - } } diff --git a/src/CSharp/EasyMicroservices.Security/EasyMicroservices.Security.csproj b/src/CSharp/EasyMicroservices.Security/EasyMicroservices.Security.csproj index 01371e2..58f025d 100644 --- a/src/CSharp/EasyMicroservices.Security/EasyMicroservices.Security.csproj +++ b/src/CSharp/EasyMicroservices.Security/EasyMicroservices.Security.csproj @@ -5,7 +5,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.1 + 0.0.0.2 encrypt encryption and security. EasyMicroservice@gmail.com encrypt,encryption,security,hash @@ -16,7 +16,7 @@ - + diff --git a/src/CSharp/EasyMicroservices.Security/ISecurityProvider.cs b/src/CSharp/EasyMicroservices.Security/ISecurityProvider.cs new file mode 100644 index 0000000..d53252d --- /dev/null +++ b/src/CSharp/EasyMicroservices.Security/ISecurityProvider.cs @@ -0,0 +1,19 @@ +namespace EasyMicroservices.Security; +/// +/// +/// +public interface ISecurityProvider +{ + /// + /// + /// + /// + /// + string ComputeHexString(byte[] buffer); + /// + /// + /// + /// + /// + byte[] Compute(byte[] buffer); +} diff --git a/src/CSharp/EasyMicroservices.Security/Interfaces/IEncryptionProvider.cs b/src/CSharp/EasyMicroservices.Security/Interfaces/IEncryptionProvider.cs index ffa8098..6c99a8c 100644 --- a/src/CSharp/EasyMicroservices.Security/Interfaces/IEncryptionProvider.cs +++ b/src/CSharp/EasyMicroservices.Security/Interfaces/IEncryptionProvider.cs @@ -8,7 +8,7 @@ namespace EasyMicroservices.Security.Interfaces /// These are a two-way process to convert your data from ///clear byte into crypto-byte and back again. /// - public interface IEncryptionProvider : IStreamMiddleware + public interface IEncryptionProvider : IStreamMiddleware, ISecurityProvider { /// /// Protection algorithms often use a key. diff --git a/src/CSharp/EasyMicroservices.Security/Interfaces/IHashProvider.cs b/src/CSharp/EasyMicroservices.Security/Interfaces/IHashProvider.cs index 898fd22..eda863c 100644 --- a/src/CSharp/EasyMicroservices.Security/Interfaces/IHashProvider.cs +++ b/src/CSharp/EasyMicroservices.Security/Interfaces/IHashProvider.cs @@ -8,7 +8,7 @@ namespace EasyMicroservices.Security.Interfaces /// This is a one-way process to generate a hash value to securely store passwords, /// or can be used to detect malicious changes or corruption of your data /// - public interface IHashProvider : IStreamMiddleware + public interface IHashProvider : IStreamMiddleware, ISecurityProvider { /// /// diff --git a/src/CSharp/EasyMicroservices.Security/Interfaces/ISignatureProvider.cs b/src/CSharp/EasyMicroservices.Security/Interfaces/ISignatureProvider.cs index 82d3d42..b36280c 100644 --- a/src/CSharp/EasyMicroservices.Security/Interfaces/ISignatureProvider.cs +++ b/src/CSharp/EasyMicroservices.Security/Interfaces/ISignatureProvider.cs @@ -9,7 +9,7 @@ namespace EasyMicroservices.Security.Interfaces ///trust by validating a signature that has been applied to some data against someone's ///public key. /// - public interface ISignatureProvider : IStreamMiddleware + public interface ISignatureProvider : IStreamMiddleware, ISecurityProvider { /// /// diff --git a/src/CSharp/EasyMicroservices.Security/Providers/BaseSecurityProvider.cs b/src/CSharp/EasyMicroservices.Security/Providers/BaseSecurityProvider.cs index 08fa906..f56b206 100644 --- a/src/CSharp/EasyMicroservices.Security/Providers/BaseSecurityProvider.cs +++ b/src/CSharp/EasyMicroservices.Security/Providers/BaseSecurityProvider.cs @@ -1,4 +1,7 @@ -using System.IO; +using System; +using System.IO; +using System.Linq; +using System.Text; using System.Threading.Tasks; namespace EasyMicroservices.Security.Providers @@ -6,7 +9,7 @@ namespace EasyMicroservices.Security.Providers /// /// /// - public abstract class BaseSecurityProvider + public abstract class BaseSecurityProvider : ISecurityProvider { /// /// @@ -25,5 +28,23 @@ public abstract class BaseSecurityProvider /// /// public abstract Task ReadFromStream(Stream streamReader); + /// + /// + /// + /// + /// + public abstract byte[] Compute(byte[] buffer); + + /// + /// + /// + /// + /// + /// + public string ComputeHexString(byte[] buffer) + { + return string.Concat(Compute(buffer) + .Select(item => item.ToString("x2"))); + } } } diff --git a/src/CSharp/EasyMicroservices.Security/Providers/EncryptionProviders/BaseEncryptionProvider.cs b/src/CSharp/EasyMicroservices.Security/Providers/EncryptionProviders/BaseEncryptionProvider.cs index b09e046..1709671 100644 --- a/src/CSharp/EasyMicroservices.Security/Providers/EncryptionProviders/BaseEncryptionProvider.cs +++ b/src/CSharp/EasyMicroservices.Security/Providers/EncryptionProviders/BaseEncryptionProvider.cs @@ -48,6 +48,17 @@ public BaseEncryptionProvider(IStreamMiddleware streamMiddleware = default) /// /// public abstract byte[] Encrypt(byte[] data); + + /// + /// + /// + /// + /// + public override byte[] Compute(byte[] buffer) + { + return Encrypt(buffer); + } + /// /// /// diff --git a/src/CSharp/EasyMicroservices.Security/Providers/HashProviders/BaseHashProvider.cs b/src/CSharp/EasyMicroservices.Security/Providers/HashProviders/BaseHashProvider.cs index 07a5cc2..05458a7 100644 --- a/src/CSharp/EasyMicroservices.Security/Providers/HashProviders/BaseHashProvider.cs +++ b/src/CSharp/EasyMicroservices.Security/Providers/HashProviders/BaseHashProvider.cs @@ -27,6 +27,16 @@ public BaseHashProvider(IStreamMiddleware streamMiddleware = default) /// public IStreamMiddleware InnerStreamMiddleware { get; set; } + /// + /// + /// + /// + /// + public override byte[] Compute(byte[] buffer) + { + return ComputeHash(buffer); + } + /// /// /// @@ -101,7 +111,7 @@ public override void WriteToStream(Stream streamWriter, byte[] data) /// public override Task ReadFromStream(Stream streamReader) { - throw new NotImplementedException(); + return streamReader.StreamToBytesAsync(1024); } } } diff --git a/src/CSharp/EasyMicroservices.Security/Providers/HashProviders/MD5HashProvider.cs b/src/CSharp/EasyMicroservices.Security/Providers/HashProviders/MD5HashProvider.cs index 1b59841..464d120 100644 --- a/src/CSharp/EasyMicroservices.Security/Providers/HashProviders/MD5HashProvider.cs +++ b/src/CSharp/EasyMicroservices.Security/Providers/HashProviders/MD5HashProvider.cs @@ -1,5 +1,6 @@ using EasyMicroservices.Security.Interfaces; using System.Security.Cryptography; +using EasyMicroservices.Utilities.IO.Interfaces; namespace EasyMicroservices.Security.Providers.HashProviders { @@ -8,6 +9,15 @@ namespace EasyMicroservices.Security.Providers.HashProviders /// public class MD5HashProvider : BaseHashProvider, IHashProvider { + /// + /// + /// + /// + public MD5HashProvider(IStreamMiddleware streamMiddleware = default) : base(streamMiddleware) + { + + } + /// /// /// diff --git a/src/CSharp/EasyMicroservices.Security/Providers/SignatureProviders/BaseSignatureProvider.cs b/src/CSharp/EasyMicroservices.Security/Providers/SignatureProviders/BaseSignatureProvider.cs index 57035cf..59dbc1d 100644 --- a/src/CSharp/EasyMicroservices.Security/Providers/SignatureProviders/BaseSignatureProvider.cs +++ b/src/CSharp/EasyMicroservices.Security/Providers/SignatureProviders/BaseSignatureProvider.cs @@ -32,6 +32,16 @@ public BaseSignatureProvider(IStreamMiddleware streamMiddleware = default) /// public IStreamMiddleware InnerStreamMiddleware { get; set; } + /// + /// + /// + /// + /// + public override byte[] Compute(byte[] buffer) + { + return SignData(buffer); + } + /// /// ///