Skip to content

Commit

Permalink
Merge pull request #18 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
Add support for ISecurityProvider
  • Loading branch information
Ali-YousefiTelori authored Jan 2, 2024
2 parents 507e610 + fe70f7e commit 7291244
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Laboratory" Version="0.0.0.6" />
<PackageReference Include="EasyMicroservices.Tests" Version="0.0.0.3" />
<PackageReference Include="EasyMicroservices.Laboratory" Version="0.0.0.16" />
<PackageReference Include="EasyMicroservices.Tests" Version="0.0.0.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit.assert" Version="2.5.0" />
</ItemGroup>

<Choose>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public virtual void Test_Symmetric_WithDifferentKey(string originalDataString)
var arrayByte = encryptedData;
// Assert
Assert.ThrowsAny<CryptographicException>(() => _anotherProvider.Decrypt(arrayByte));

Assert.True(encryptedData.SequenceEqual(_provider.Compute(data)));
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EasyMicroservices.Security.Interfaces;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -29,6 +30,7 @@ public virtual void SignatureAlgorithmSignAndValidateDataSuccess(string dataStri

// assert
Assert.True(isValid);
Assert.True(signature.SequenceEqual(_provider.Compute(data)));
}

[Theory]
Expand All @@ -46,6 +48,7 @@ public virtual async Task SignatureAlgorithmSignAndValidateDataSuccessStream(str

// assert
Assert.True(isValid);
Assert.True(signature.SequenceEqual(_provider.Compute(data)));
}

[Theory]
Expand All @@ -62,7 +65,7 @@ public virtual void SignatureAlgorithmSignAndValidateDataFalse(string dataString

// assert
Assert.False(isValid);
Assert.True(signature.SequenceEqual(_provider.Compute(data)));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.1</Version>
<Version>0.0.0.2</Version>
<Description>encrypt encryption and security.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>encrypt,encryption,security,hash</PackageTags>
Expand All @@ -16,7 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Utilities" Version="0.0.0.11" />
<PackageReference Include="EasyMicroservices.Utilities" Version="0.0.0.13" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions src/CSharp/EasyMicroservices.Security/ISecurityProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace EasyMicroservices.Security;
/// <summary>
///
/// </summary>
public interface ISecurityProvider
{
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
string ComputeHexString(byte[] buffer);
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
byte[] Compute(byte[] buffer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
public interface IEncryptionProvider : IStreamMiddleware
public interface IEncryptionProvider : IStreamMiddleware, ISecurityProvider
{
/// <summary>
/// Protection algorithms often use a key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// </summary>
public interface IHashProvider : IStreamMiddleware
public interface IHashProvider : IStreamMiddleware, ISecurityProvider
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
public interface ISignatureProvider : IStreamMiddleware
public interface ISignatureProvider : IStreamMiddleware, ISecurityProvider
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyMicroservices.Security.Providers
{
/// <summary>
///
/// </summary>
public abstract class BaseSecurityProvider
public abstract class BaseSecurityProvider : ISecurityProvider
{
/// <summary>
///
Expand All @@ -25,5 +28,23 @@ public abstract class BaseSecurityProvider
/// <param name="streamReader"></param>
/// <returns></returns>
public abstract Task<byte[]> ReadFromStream(Stream streamReader);
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public abstract byte[] Compute(byte[] buffer);

/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
/// <exception cref="System.NotImplementedException"></exception>
public string ComputeHexString(byte[] buffer)
{
return string.Concat(Compute(buffer)
.Select(item => item.ToString("x2")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public BaseEncryptionProvider(IStreamMiddleware streamMiddleware = default)
/// <param name="data"></param>
/// <returns></returns>
public abstract byte[] Encrypt(byte[] data);

/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public override byte[] Compute(byte[] buffer)
{
return Encrypt(buffer);
}

/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public BaseHashProvider(IStreamMiddleware streamMiddleware = default)
/// </summary>
public IStreamMiddleware InnerStreamMiddleware { get; set; }

/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public override byte[] Compute(byte[] buffer)
{
return ComputeHash(buffer);
}

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -101,7 +111,7 @@ public override void WriteToStream(Stream streamWriter, byte[] data)
/// <exception cref="NotImplementedException"></exception>
public override Task<byte[]> ReadFromStream(Stream streamReader)
{
throw new NotImplementedException();
return streamReader.StreamToBytesAsync(1024);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EasyMicroservices.Security.Interfaces;
using System.Security.Cryptography;
using EasyMicroservices.Utilities.IO.Interfaces;

namespace EasyMicroservices.Security.Providers.HashProviders
{
Expand All @@ -8,6 +9,15 @@ namespace EasyMicroservices.Security.Providers.HashProviders
/// </summary>
public class MD5HashProvider : BaseHashProvider, IHashProvider
{
/// <summary>
///
/// </summary>
/// <param name="streamMiddleware"></param>
public MD5HashProvider(IStreamMiddleware streamMiddleware = default) : base(streamMiddleware)
{

}

/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public BaseSignatureProvider(IStreamMiddleware streamMiddleware = default)
/// </summary>
public IStreamMiddleware InnerStreamMiddleware { get; set; }

/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public override byte[] Compute(byte[] buffer)
{
return SignData(buffer);
}

/// <summary>
///
/// </summary>
Expand Down

0 comments on commit 7291244

Please sign in to comment.