Skip to content

Commit

Permalink
masking class added
Browse files Browse the repository at this point in the history
  • Loading branch information
Haik committed Jan 23, 2024
1 parent 909d0ba commit c2f9e31
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 22 deletions.
65 changes: 45 additions & 20 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
# 1. PandaTech.Crypto


- [1. PandaTech.Crypto](#1-pandatechcrypto)
- [1.1. Introduction](#11-introduction)
- [1.2. Features](#12-features)
- [1.3. Installation](#13-installation)
- [1.4. How to Use](#14-how-to-use)
- [1.4.1. Configuring Dependency Injection](#141-configuring-dependency-injection)
- [1.4.2. AES256 Class](#142-aes256-class)
- [1.4.2.1. Immutable Configurations](#1421-immutable-configurations)
- [1.4.2.2. Encryption/Decryption methods with hashing](#1422-encryptiondecryption-methods-with-hashing)
- [1.4.2.3. Encryption/Decryption methods without hashing](#1423-encryptiondecryption-methods-without-hashing)
- [1.4.2.4. Encryption/Decryption methods with custom key (overriding options for one time)](#1424-encryptiondecryption-methods-with-custom-key-overriding-options-for-one-time)
- [1.4.3. Argon2id Class](#143-argon2id-class)
- [1.4.3.1. Default Configurations](#1431-default-configurations)
- [1.4.3.2 Hash password and verify hash](#1432-hash-password-and-verify-hash)
- [1.4.4. Random Class](#144-random-class)
- [1.4.5. Password Class](#145-password-class)
- [1.4.6. Sha3 Class](#146-sha3-class)
- [1.4.7. GZip Class](#147-gzip-class)
- [1.5. License](#15-license)
- [1.1. Introduction](#11-introduction)
- [1.2. Features](#12-features)
- [1.3. Installation](#13-installation)
- [1.4. How to Use](#14-how-to-use)
- [1.4.1. Configuring Dependency Injection](#141-configuring-dependency-injection)
- [1.4.2. AES256 Class](#142-aes256-class)
- [1.4.2.1. Immutable Configurations](#1421-immutable-configurations)
- [1.4.2.2. Encryption/Decryption methods with hashing](#1422-encryptiondecryption-methods-with-hashing)
- [1.4.2.3. Encryption/Decryption methods without hashing](#1423-encryptiondecryption-methods-without-hashing)
- [1.4.2.4. Encryption/Decryption methods with custom key (overriding options for one time)](#1424-encryptiondecryption-methods-with-custom-key-overriding-options-for-one-time)
- [1.4.3. Argon2id Class](#143-argon2id-class)
- [1.4.3.1. Default Configurations](#1431-default-configurations)
- [1.4.3.2 Hash password and verify hash](#1432-hash-password-and-verify-hash)
- [1.4.4. Random Class](#144-random-class)
- [1.4.5. Password Class](#145-password-class)
- [1.4.6. Sha3 Class](#146-sha3-class)
- [1.4.7. GZip Class](#147-gzip-class)
- [1.4.8. Mask Class](#148-mask-class)
- [1.4.8.1. Masking Email Addresses](#1481-masking-email-addresses)
- [1.5. License](#15-license)

## 1.1. Introduction

Expand All @@ -44,6 +45,8 @@ resources—hash generation takes under 500ms on a container with 1 vCore and 1G
specific character sets.
* **GZip Compression/Decompression:** Efficiently compress and decompress data using GZip, with support for byte arrays
and streams.
* **Masking:** Mask sensitive information like email addresses and phone numbers, ensuring that they are partially
hidden and thus safeguarded.
* **Performance Optimized:** Tested to run efficiently in resource-constrained environments.
* **High Test Coverage:** Confidence backed by 99% unit test coverage.

Expand Down Expand Up @@ -163,9 +166,11 @@ var isHashValid = Sha3.VerifyHash("yourPlainText", sha3Hash);
### 1.4.7. GZip Class

Compression and Decompression
The `GZip` class provides methods for compressing and decompressing data using GZip. It supports operations on strings, byte arrays, and streams.
The `GZip` class provides methods for compressing and decompressing data using GZip. It supports operations on strings,
byte arrays, and streams.

Example usage for compressing and decompressing a string:

```csharp
using Pandatech.Crypto;

Expand All @@ -175,7 +180,27 @@ byte[] compressedData = GZip.Compress(data);

// Decompress back to string
string decompressedData = Encoding.UTF8.GetString(GZip.Decompress(compressedData));
```

### 1.4.8. Mask Class

The `Mask` class in the PandaTech.Crypto library provides methods to mask sensitive information like email addresses and
phone numbers, ensuring that they are partially hidden and thus safeguarded.

#### 1.4.8.1. Masking Email Addresses

The `MaskEmail` method masks the local part of an email address, showing only the first two characters and replacing the
rest with asterisks (*), keeping the domain part intact.

```csharp
// Example usage for masking an email
string maskedEmail = Mask.MaskEmail("[email protected]");

// Output: "ex*****@email.com"
// Example usage for masking a phone number
string maskedPhone = Mask.MaskPhoneNumber("1234567890");

// Output: "******7890"
```

## 1.5. License
Expand Down
45 changes: 45 additions & 0 deletions src/Pandatech.Crypto.Tests/MaskTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Pandatech.Crypto.Tests;

using Xunit;
using Crypto;
using System;

public class MaskTests
{
[Theory]
[InlineData("[email protected]", "va***************@vazgen.com")]
[InlineData("[email protected]", "te**@example.com")]
[InlineData("[email protected]", "[email protected]")]
[InlineData("[email protected]", "[email protected]")]
public void MaskEmail_ValidEmails_ReturnsMaskedEmail(string input, string expected)
{
var result = Mask.MaskEmail(input);
Assert.Equal(expected, result);
}

[Theory]
[InlineData("")]
[InlineData("notanemail")]
public void MaskEmail_InvalidEmails_ThrowsArgumentException(string input)
{
Assert.Throws<ArgumentException>(() => Mask.MaskEmail(input));
}

[Theory]
[InlineData("1234567890", "******7890")]
[InlineData("1234", "1234")]
[InlineData("12", "12")]
public void MaskPhoneNumber_ValidPhoneNumbers_ReturnsMaskedPhone(string input, string expected)
{
var result = Mask.MaskPhoneNumber(input);
Assert.Equal(expected, result);
}

[Theory]
[InlineData(null)]
[InlineData("")]
public void MaskPhoneNumber_InvalidPhoneNumbers_ThrowsArgumentException(string input)
{
Assert.Throws<ArgumentException>(() => Mask.MaskPhoneNumber(input));
}
}
34 changes: 34 additions & 0 deletions src/Pandatech.Crypto/Mask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using RegexBox;

namespace Pandatech.Crypto;

public static class Mask
{
public static string MaskEmail(string email)
{
if (!PandaValidator.IsEmail(email))
{
throw new ArgumentException("Invalid email address", nameof(email));
}

var parts = email.Split('@');
var localPart = parts[0];
var domainPart = parts[1];

var maskedLocalPart =
localPart.Length <= 2 ? localPart : localPart[..2] + new string('*', localPart.Length - 2);
return $"{maskedLocalPart}@{domainPart}";
}

public static string MaskPhoneNumber(string phoneNumber)
{
if (string.IsNullOrEmpty(phoneNumber))
{
throw new ArgumentException("Invalid phone number", nameof(phoneNumber));
}

return phoneNumber.Length <= 4
? phoneNumber
: string.Concat(new string('*', phoneNumber.Length - 4), phoneNumber.AsSpan(phoneNumber.Length - 4));
}
}
5 changes: 3 additions & 2 deletions src/Pandatech.Crypto/Pandatech.Crypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<Copyright>MIT</Copyright>
<PackageIcon>pandatech.png</PackageIcon>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Version>2.2.4</Version>
<Version>2.2.5</Version>
<Title>Pandatech.Crypto</Title>
<PackageTags>Pandatech, library, encryption, hash, algorythms, security</PackageTags>
<Description>PandaTech.Crypto is a .NET library simplifying common cryptograhic functions.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-pandatech-crypto</RepositoryUrl>
<PackageReleaseNotes>Added GZip compression</PackageReleaseNotes>
<PackageReleaseNotes>Added Mask class</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand All @@ -25,6 +25,7 @@
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" />
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Pandatech.RegexBox" Version="1.1.4" />
</ItemGroup>

</Project>

0 comments on commit c2f9e31

Please sign in to comment.