Skip to content

Commit

Permalink
Updated AES256 byte array sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
Haik committed Oct 30, 2023
1 parent c9b5a86 commit 5ea547f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 33 deletions.
8 changes: 4 additions & 4 deletions Pandatech.Crypto/Aes256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ public static byte[] EncryptWithHash(string plainText, string key)
{
var encryptedBytes = Encrypt(plainText, key);
var hashBytes = Sha3.Hash(plainText);
return encryptedBytes.Concat(hashBytes).ToArray();
return hashBytes.Concat(encryptedBytes).ToArray();
}

public static string DecryptIgnoringHash(byte[] cipherTextWithHash)
public static string DecryptIgnoringHash(IEnumerable<byte> cipherTextWithHash)
{
return DecryptIgnoringHash(cipherTextWithHash, Key);
}

public static string DecryptIgnoringHash(byte[] cipherTextWithHash, string key)
public static string DecryptIgnoringHash(IEnumerable<byte> cipherTextWithHash, string key)
{
var cipherText = cipherTextWithHash.Take(cipherTextWithHash.Length - HashSize).ToArray();
var cipherText = cipherTextWithHash.Skip(HashSize).ToArray();
return Decrypt(cipherText, key);
}

Expand Down
4 changes: 2 additions & 2 deletions Pandatech.Crypto/Pandatech.Crypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.1.6</Version>
<Version>2.0.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>Refactored RandomPassword class logic</PackageReleaseNotes>
<PackageReleaseNotes>AES256 encrypted byte array sequence changed</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
53 changes: 41 additions & 12 deletions Pandatech.Crypto/Readme.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
# PandaTech.Crypto

## Introduction
Pandatech.Crypto is a powerful cryptographic utility library backed by 99% test coverage through unit tests. The library offers an array of static methods for secure data operations, including AES256 encryption and decryption, Argon2Id password hashing and verification, as well as utilities for generating cryptographic random bytes and passwords.

Designed to work efficiently in containerized environments, the library performs effectively even with limited resources—hash generation takes under 500ms on a container with 1 vCore and 1GB of RAM.
Pandatech.Crypto is a powerful cryptographic utility library backed by 99% test coverage through unit tests. The library
offers an array of static methods for secure data operations, including AES256 encryption and decryption, Argon2Id
password hashing and verification, as well as utilities for generating cryptographic random bytes and passwords.

Designed to work efficiently in containerized environments, the library performs effectively even with limited
resources—hash generation takes under 500ms on a container with 1 vCore and 1GB of RAM.

## Features
* **AES 256-bit Encryption/Decryption:** Encrypt your data and get the IV and encrypted bytes in one array. Decrypt it back to its original form, seamlessly handling the IV.
* **Argon2Id Hashing:** Hash and verify passwords securely with an immutable configuration that's optimized for performance.

* **AES 256-bit Encryption/Decryption:** Encrypt your data and get the IV and encrypted bytes in one array. Decrypt it
back to its original form, seamlessly handling the IV. Note that you have option to encrypt with hash and decrypt
ignoring hash. (for cases where you want to apply filtering on the encrypted data or check uniqueness of the encrypted
data)
* **Argon2Id Hashing:** Hash and verify passwords securely with an immutable configuration that's optimized for
performance.
* **SHA-3 Hashing:** Generate and verify SHA-3 hashes with 512-bit output.
* **Random Number/Password Generation:** Generate cryptographic random bytes, AES256 keys, or strong passwords with specific character sets.
* **Random Number/Password Generation:** Generate cryptographic random bytes, AES256 keys, or strong passwords with
specific character sets.
* **Performance Optimized:** Tested to run efficiently in resource-constrained environments.
* **High Test Coverage:** Confidence backed by 99% unit test coverage.

## Installation

To use `PandaTech.Crypto` in your project, install the NuGet package using the following command in the Package Manager Console:
`Install-Package PandaTech.Crypto` or, search for "PandaTech.Crypto" in the NuGet Package Manager and install it from there.
To use `PandaTech.Crypto` in your project, install the NuGet package using the following command in the Package Manager
Console:
`Install-Package PandaTech.Crypto` or, search for "PandaTech.Crypto" in the NuGet Package Manager and install it from
there.

## How to Use

### 1. AES256 Class

#### Configurations

1. **IV**: A random IV is generated for each Encryption, enhancing security.
2. **PaddingMode**: PKCS7

#### Methods

1. **Encrypt(string plainText)**: Encrypts a plain text using a default environment variable key.
2. **Encrypt(string plainText, string key)**: Encrypts a plain text using a given key.
3. **Decrypt(byte[] cipherText)**: Decrypts to plain text using a default environment variable key.
4. **Decrypt(byte[] cipherText, string key)**: Decrypts to plain text using a given key.
5. **EncryptWithHash(string plainText)**: Encrypts and appends SHA-3 hash. (for cases where you want to apply filtering on the encrypted data)
6. **DecryptIgnoringHash(byte[] cipherTextWithHash)**: Decrypts while ignoring SHA-3 hash. (for cases where you want to apply filtering on the encrypted data)
7. **EncryptWithHash(string plainText, string key)**: Encrypts using a given key and appends SHA-3 hash. (for cases where you want to apply filtering on the encrypted data)
8. **DecryptIgnoringHash(byte[] cipherTextWithHash, string key)**: Decrypts using a given key while ignoring SHA-3 hash. (for cases where you want to apply filtering on the encrypted data)
5. **EncryptWithHash(string plainText)**: Encrypts and appends SHA-3 hash. (for cases where you want to apply filtering
on the encrypted data)
6. **DecryptIgnoringHash(byte[] cipherTextWithHash)**: Decrypts while ignoring SHA-3 hash. (for cases where you want to
apply filtering on the encrypted data)
7. **EncryptWithHash(string plainText, string key)**: Encrypts using a given key and appends SHA-3 hash. (for cases
where you want to apply filtering on the encrypted data)
8. **DecryptIgnoringHash(byte[] cipherTextWithHash, string key)**: Decrypts using a given key while ignoring SHA-3
hash. (for cases where you want to apply filtering on the encrypted data)
Encryption and decryption with environment variable key

```csharp
// Example for basic encryption and decryption
Environment.SetEnvironmentVariable("AES_KEY", Random.GenerateAes256KeyString());
Expand All @@ -47,27 +69,33 @@ var decryptedIgnoringHash = Aes256.DecryptIgnoringHash(encryptedWithHash);
```

### 2. Argon2id Class

#### Configurations

1. **Salt**: A random salt is generated for each password hash, enhancing security.
2. **DegreeOfParallelism**: 8
3. **Iterations**: 5
4. **MemorySize**: 128 MB

Hash password and verify hash

```csharp
// Example usage for hashing
var hashedPassword = Argon2Id.HashPassword("yourPassword");

// Example usage for verifying a hash
var isPasswordValid = Argon2Id.VerifyHash("yourPassword", hashedPassword);
```

### 3. Random Class

```csharp
var randomBytes = Random.GenerateBytes(16);
var aesKey = Random.GenerateAes256KeyString();
```

### 4. RandomPassword Class

```csharp
var includeUppercase = true;
var includeLowercase = true;
Expand All @@ -77,6 +105,7 @@ string password = RandomPassword.Generate(16, includeUppercase, includeLowercase
```

### 5. Sha3 Class

```csharp
// Example usage for generating hash
var sha3Hash = Sha3.Hash("yourPlainText");
Expand All @@ -85,8 +114,8 @@ var sha3Hash = Sha3.Hash("yourPlainText");
var isHashValid = Sha3.VerifyHash("yourPlainText", sha3Hash);
```


## License

PandaTech.Crypto is licensed under the MIT License.

Your Security, Our Priority.
59 changes: 44 additions & 15 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
# PandaTech.Crypto

## Introduction
Pandatech.Crypto is a powerful cryptographic utility library backed by 99% test coverage through unit tests. The library offers an array of static methods for secure data operations, including AES256 encryption and decryption, Argon2Id password hashing and verification, as well as utilities for generating cryptographic random bytes and passwords.

Designed to work efficiently in containerized environments, the library performs effectively even with limited resources—hash generation takes under 500ms on a container with 1 vCore and 1GB of RAM.
Pandatech.Crypto is a powerful cryptographic utility library backed by 99% test coverage through unit tests. The library
offers an array of static methods for secure data operations, including AES256 encryption and decryption, Argon2Id
password hashing and verification, as well as utilities for generating cryptographic random bytes and passwords.

Designed to work efficiently in containerized environments, the library performs effectively even with limited
resources—hash generation takes under 500ms on a container with 1 vCore and 1GB of RAM.

## Features
* **AES 256-bit Encryption/Decryption:** Encrypt your data and get the IV and encrypted bytes in one array. Decrypt it back to its original form, seamlessly handling the IV.
* **Argon2Id Hashing:** Hash and verify passwords securely with an immutable configuration that's optimized for performance.

* **AES 256-bit Encryption/Decryption:** Encrypt your data and get the IV and encrypted bytes in one array. Decrypt it
back to its original form, seamlessly handling the IV. Note that you have option to encrypt with hash and decrypt
ignoring hash. (for cases where you want to apply filtering on the encrypted data or check uniqueness of the encrypted
data)
* **Argon2Id Hashing:** Hash and verify passwords securely with an immutable configuration that's optimized for
performance.
* **SHA-3 Hashing:** Generate and verify SHA-3 hashes with 512-bit output.
* **Random Number/Password Generation:** Generate cryptographic random bytes, AES256 keys, or strong passwords with specific character sets.
* **Random Number/Password Generation:** Generate cryptographic random bytes, AES256 keys, or strong passwords with
specific character sets.
* **Performance Optimized:** Tested to run efficiently in resource-constrained environments.
* **High Test Coverage:** Confidence backed by 99% unit test coverage.

## Installation

To use `PandaTech.Crypto` in your project, install the NuGet package using the following command in the Package Manager Console:
`Install-Package PandaTech.Crypto` or, search for "PandaTech.Crypto" in the NuGet Package Manager and install it from there.
To use `PandaTech.Crypto` in your project, install the NuGet package using the following command in the Package Manager
Console:
`Install-Package PandaTech.Crypto` or, search for "PandaTech.Crypto" in the NuGet Package Manager and install it from
there.

## How to Use

### 1. AES256 Class

#### Configurations

1. **IV**: A random IV is generated for each Encryption, enhancing security.
2. **PaddingMode**: PKCS7

#### Methods

1. **Encrypt(string plainText)**: Encrypts a plain text using a default environment variable key.
2. **Encrypt(string plainText, string key)**: Encrypts a plain text using a given key.
3. **Decrypt(byte[] cipherText)**: Decrypts to plain text using a default environment variable key.
4. **Decrypt(byte[] cipherText, string key)**: Decrypts to plain text using a given key.
5. **EncryptWithHash(string plainText)**: Encrypts and appends SHA-3 hash. (for cases where you want to apply filtering on the encrypted data)
6. **DecryptIgnoringHash(byte[] cipherTextWithHash)**: Decrypts while ignoring SHA-3 hash. (for cases where you want to apply filtering on the encrypted data)
7. **EncryptWithHash(string plainText, string key)**: Encrypts using a given key and appends SHA-3 hash. (for cases where you want to apply filtering on the encrypted data)
8. **DecryptIgnoringHash(byte[] cipherTextWithHash, string key)**: Decrypts using a given key while ignoring SHA-3 hash. (for cases where you want to apply filtering on the encrypted data)
Encryption and decryption with environment variable key
5. **EncryptWithHash(string plainText)**: Encrypts and appends SHA-3 hash. (for cases where you want to apply filtering
on the encrypted data)
6. **DecryptIgnoringHash(byte[] cipherTextWithHash)**: Decrypts while ignoring SHA-3 hash. (for cases where you want to
apply filtering on the encrypted data)
7. **EncryptWithHash(string plainText, string key)**: Encrypts using a given key and appends SHA-3 hash. (for cases
where you want to apply filtering on the encrypted data)
8. **DecryptIgnoringHash(byte[] cipherTextWithHash, string key)**: Decrypts using a given key while ignoring SHA-3
hash. (for cases where you want to apply filtering on the encrypted data)
Encryption and decryption with environment variable key

```csharp
// Example for basic encryption and decryption
Environment.SetEnvironmentVariable("AES_KEY", Random.GenerateAes256KeyString());
Expand All @@ -47,27 +69,33 @@ var decryptedIgnoringHash = Aes256.DecryptIgnoringHash(encryptedWithHash);
```

### 2. Argon2id Class

#### Configurations

1. **Salt**: A random salt is generated for each password hash, enhancing security.
2. **DegreeOfParallelism**: 8
3. **Iterations**: 5
2. **DegreeOfParallelism**: 8
3. **Iterations**: 5
4. **MemorySize**: 128 MB

Hash password and verify hash

```csharp
// Example usage for hashing
var hashedPassword = Argon2Id.HashPassword("yourPassword");

// Example usage for verifying a hash
var isPasswordValid = Argon2Id.VerifyHash("yourPassword", hashedPassword);
```

### 3. Random Class

```csharp
var randomBytes = Random.GenerateBytes(16);
var aesKey = Random.GenerateAes256KeyString();
```

### 4. RandomPassword Class

```csharp
var includeUppercase = true;
var includeLowercase = true;
Expand All @@ -77,6 +105,7 @@ string password = RandomPassword.Generate(16, includeUppercase, includeLowercase
```

### 5. Sha3 Class

```csharp
// Example usage for generating hash
var sha3Hash = Sha3.Hash("yourPlainText");
Expand All @@ -85,8 +114,8 @@ var sha3Hash = Sha3.Hash("yourPlainText");
var isHashValid = Sha3.VerifyHash("yourPlainText", sha3Hash);
```


## License

PandaTech.Crypto is licensed under the MIT License.

Your Security, Our Priority.

0 comments on commit 5ea547f

Please sign in to comment.