Skip to content

Commit

Permalink
Testings added
Browse files Browse the repository at this point in the history
  • Loading branch information
Haik committed Oct 14, 2023
1 parent ec4abb5 commit 12cb9b2
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 15 deletions.
12 changes: 12 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-stryker": {
"version": "3.10.0",
"commands": [
"dotnet-stryker"
]
}
}
}
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ jobs:
dotnet-version: ${{ matrix.dotnet-version }}

- name: Build
run: dotnet build ./Pandatech.Crypto
run: dotnet build ./Pandatech.Crypto/Pandatech.Crypto.csproj

- name: Test
run: dotnet test ./Pandatech.Crypto --collect:"XPlat Code Coverage"
run: dotnet test ./Pandatech.Crypto/Pandatech.Crypto.csproj --collect:"XPlat Code Coverage"

- name: Pack
run: dotnet pack ./Pandatech.Crypto --output nupkgs
run: dotnet pack ./Pandatech.Crypto/Pandatech.Crypto.csproj --output nupkgs

- name: Publish
run: dotnet nuget push ./nupkgs/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
55 changes: 51 additions & 4 deletions Pandatech.Crypto.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Pandatech.Cryptos;
using Random = Pandatech.Cryptos.Random;

namespace Pandatech.Crypto.Tests;

public class UnitTests
Expand All @@ -9,7 +6,7 @@ public class UnitTests
public void Generate_ShouldReturnByteArray()
{
const int length = 16;
var randomBytes = Cryptos.Random.GenerateBytes(length);
var randomBytes = Random.GenerateBytes(length);

Assert.NotNull(randomBytes);
Assert.Equal(length, randomBytes.Length);
Expand Down Expand Up @@ -83,4 +80,54 @@ public void EncryptDecryptWithoutParameter_ShouldReturnOriginalString()

Assert.Equal(original, decrypted);
}

[Fact]
public void EncryptDecryptWithInvalidKey_ShouldThrowException()
{
const string invalidKey = "InvalidKey";
const string original = "MySensitiveData";

Assert.Throws<ArgumentException>(() => Aes256.Encrypt(original, invalidKey));
Assert.Throws<ArgumentException>(() => Aes256.Decrypt(Array.Empty<byte>(), invalidKey));
}

[Fact]
public void EncryptDecryptWithShortKey_ShouldThrowException()
{
var shortKey = Convert.ToBase64String(new byte[15]); // Less than 256 bits
const string original = "MySensitiveData";

Assert.Throws<ArgumentException>(() => Aes256.Encrypt(original, shortKey));
Assert.Throws<ArgumentException>(() => Aes256.Decrypt(Array.Empty<byte>(), shortKey));
}

[Fact]
public void EncryptDecryptWithEmptyText_ShouldThrowException()
{
var key = Random.GenerateAes256KeyString();
var original = string.Empty;

Assert.Throws<ArgumentException>(() => Aes256.Encrypt(original, key));
Assert.Throws<ArgumentException>(() => Aes256.Decrypt(Array.Empty<byte>(), key));
}

[Fact]
public void EncryptDecryptWithNullCipher_ShouldThrowException()
{
var key = Random.GenerateAes256KeyString();

Assert.Throws<ArgumentException>(() => Aes256.Decrypt(null!, key));
}

[Fact]
public void HashPassword_EmptyPassword_ShouldThrowException()
{
Assert.Throws<ArgumentException>(() => Argon2Id.HashPassword(""));
}

[Fact]
public void VerifyHash_NullHash_ShouldThrowException()
{
Assert.Throws<ArgumentException>(() => Argon2Id.VerifyHash("password", null!));
}
}
28 changes: 27 additions & 1 deletion Pandatech.Crypto/Aes256.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Security.Cryptography;

namespace Pandatech.Cryptos;
namespace Pandatech.Crypto;

public static class Aes256
{
Expand All @@ -15,6 +15,7 @@ public static byte[] Encrypt(string plainText)

public static byte[] Encrypt(string plainText, string key)
{
ValidateInputs(plainText, key);
using var aesAlg = Aes.Create();
aesAlg.KeySize = KeySize;
aesAlg.Padding = PaddingMode.PKCS7;
Expand Down Expand Up @@ -43,6 +44,7 @@ public static string Decrypt(byte[] cipherText)

public static string Decrypt(byte[] cipherText, string key)
{
ValidateInputs(cipherText, key);
var iv = cipherText.Take(IvSize).ToArray();
var encrypted = cipherText.Skip(IvSize).ToArray();

Expand All @@ -59,4 +61,28 @@ public static string Decrypt(byte[] cipherText, string key)
using var srDecrypt = new StreamReader(csDecrypt);
return srDecrypt.ReadToEnd();
}

private static void ValidateInputs(string text, string key)
{
if (string.IsNullOrEmpty(text))
throw new ArgumentException("Text cannot be null or empty.");

if (string.IsNullOrEmpty(key) || !IsBase64String(key) || Convert.FromBase64String(key).Length != 32)
throw new ArgumentException("Invalid key.");
}

private static void ValidateInputs(byte[] cipherText, string key)
{
if (cipherText == null || cipherText.Length < IvSize)
throw new ArgumentException("Invalid cipher text.");

if (string.IsNullOrEmpty(key) || !IsBase64String(key) || Convert.FromBase64String(key).Length != 32)
throw new ArgumentException("Invalid key.");
}

private static bool IsBase64String(string s)
{
var buffer = new Span<byte>(new byte[s.Length]);
return Convert.TryFromBase64String(s, buffer, out _);
}
}
14 changes: 11 additions & 3 deletions Pandatech.Crypto/Argon2Id.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Security.Cryptography;
using System.Text;
using System.Text;
using Konscious.Security.Cryptography;

namespace Pandatech.Cryptos;
namespace Pandatech.Crypto;

public static class Argon2Id
{
Expand All @@ -14,6 +13,10 @@ public static class Argon2Id

public static byte[] HashPassword(string password)
{
if (string.IsNullOrEmpty(password))
{
throw new ArgumentException("Password cannot be null or empty.", nameof(password));
}
var salt = Random.GenerateBytes(SaltSize);
return HashPassword(password, salt);
}
Expand All @@ -35,6 +38,11 @@ private static byte[] HashPassword(string password, byte[] salt)

public static bool VerifyHash(string password, byte[] hash)
{
if (hash == null || hash.Length <= SaltSize)
{
throw new ArgumentException($"Hash must be at least {SaltSize} bytes.", nameof(hash));
}

var salt = hash.Take(SaltSize).ToArray();

var newHash = HashPassword(password, salt);
Expand Down
32 changes: 32 additions & 0 deletions Pandatech.Crypto/Pandatech.Crypto.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.1.0</Version>
<Title>Pandatech.Crypto</Title>
<Authors>Pandatech</Authors>
<PackageIcon>pandatech.png</PackageIcon>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Description>PandaTech.Crypto is a .NET library simplifying common cryptograhic functions.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-pandatech-crypto</RepositoryUrl>
<PackageReleaseNotes>Refactoring project</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
<None Update="pandatech.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Update="Readme.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Pandatech.Crypto/Random.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Security.Cryptography;

namespace Pandatech.Cryptos;
namespace Pandatech.Crypto;

public static class Random
{
Expand Down
4 changes: 1 addition & 3 deletions Pandatech.Crypto/RandomPassword.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Security.Cryptography;

namespace Pandatech.Cryptos;
namespace Pandatech.Crypto;

public static class RandomPassword
{
Expand Down

0 comments on commit 12cb9b2

Please sign in to comment.