Skip to content

Commit

Permalink
Merge pull request #1 from DevsDaddy/develop
Browse files Browse the repository at this point in the history
Add first release of Crypto Library
  • Loading branch information
DevsDaddy authored Mar 8, 2024
2 parents 7bcb909 + d4b7091 commit 2cf931b
Show file tree
Hide file tree
Showing 99 changed files with 4,002 additions and 2 deletions.
3 changes: 3 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/Core.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/Core/ICryptoProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace DevsDaddy.Shared.CryptoLibrary.Core
{
/// <summary>
/// Base Crypto-Provider Interface
/// </summary>
public interface ICryptoProvider
{
/// <summary>
/// Decrypt String Data
/// </summary>
/// <param name="encryptedString"></param>
/// <returns></returns>
string DecryptString(string encryptedString);

/// <summary>
/// Decrypt Binary Data
/// </summary>
/// <param name="encryptedBinary"></param>
/// <returns></returns>
byte[] DecryptBinary(byte[] encryptedBinary);

/// <summary>
/// Encrypt String
/// </summary>
/// <param name="decryptedString"></param>
/// <returns></returns>
string EncryptString(string decryptedString);

/// <summary>
/// Encrypt Binary
/// </summary>
/// <param name="decryptedBinary"></param>
/// <returns></returns>
byte[] EncryptBinary(byte[] decryptedBinary);
}
}
3 changes: 3 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/Core/ICryptoProvider.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/Core/IProviderOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DevsDaddy.Shared.CryptoLibrary.Core
{
/// <summary>
/// Provider Options Interface
/// </summary>
public interface IProviderOptions { }
}
3 changes: 3 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/Core/IProviderOptions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/CryptoController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using DevsDaddy.Shared.CryptoLibrary.Core;
using DevsDaddy.Shared.CryptoLibrary.Modules.AES;

namespace DevsDaddy.Shared.CryptoLibrary
{
public static class CryptoController
{
// Default Provider
private static ICryptoProvider defaultProvider = new AESProvider();

/// <summary>
/// Set Default Crypto Provider
/// </summary>
/// <param name="provider"></param>
public static void SetDefaultProvider(ICryptoProvider provider) {
defaultProvider = provider;
}

/// <summary>
/// Decrypt Encrypted Plain-Text using provided Crypto-provider or Default Crypto-provider
/// </summary>
/// <param name="encryptedString"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static string Decrypt(string encryptedString, ICryptoProvider provider = null) {
if (provider == null) provider = defaultProvider;
return provider.DecryptString(encryptedString);
}

/// <summary>
/// Decrypt encrypted byte array using provided Crypto-provider or Default Crypto-provider
/// </summary>
/// <param name="encryptedArray"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static byte[] Decrypt(byte[] encryptedArray, ICryptoProvider provider) {
if (provider == null) provider = defaultProvider;
return provider.DecryptBinary(encryptedArray);
}

/// <summary>
/// Encrypt plain-text using provided Crypto-provider or Default Crypto-provider
/// </summary>
/// <param name="plainText"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static string Encrypt(string plainText, ICryptoProvider provider = null) {
if (provider == null) provider = defaultProvider;
return provider.EncryptString(plainText);
}

/// <summary>
/// Encrypt binary array using provided Crypto-provider or Default Crypto-provider
/// </summary>
/// <param name="decryptedArray"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static byte[] Encrypt(byte[] decryptedArray, ICryptoProvider provider = null) {
if (provider == null) provider = defaultProvider;
return provider.EncryptBinary(decryptedArray);
}

public static void Test() {
CryptoFile.SetDefaultProvider(new AESProvider(new AESEncryptionOptions {
cryptoKey = "key"
}));

string decryptedText = CryptoFile.ReadText("path_to_encrypted_file");
bool writtenFile = CryptoFile.WriteText("path_to_encrypted_file", decryptedText);
}
}
}
3 changes: 3 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/CryptoController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

180 changes: 180 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/CryptoFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using DevsDaddy.Shared.CryptoLibrary.Core;
using DevsDaddy.Shared.CryptoLibrary.Modules.AES;
using UnityEngine;

namespace DevsDaddy.Shared.CryptoLibrary
{
public static class CryptoFile
{
// Default Provider
private static ICryptoProvider defaultProvider = new AESProvider();

/// <summary>
/// Set Default Crypto Provider
/// </summary>
/// <param name="provider"></param>
public static void SetDefaultProvider(ICryptoProvider provider) {
defaultProvider = provider;
}

/// <summary>
/// Write Text file to path with encrypted plain-text using provided / default crypto-provider
/// </summary>
/// <param name="path"></param>
/// <param name="plainText"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static bool WriteText(string path, string plainText, ICryptoProvider provider = null) {
try {
if (provider == null) provider = defaultProvider;
string encryptedText = provider.EncryptString(plainText);
File.WriteAllText(path, encryptedText);
return true;
}
catch (Exception ex) {
Debug.LogError(ex);
return false;
}
}

/// <summary>
/// Write Binary File to path encrypted byte array using provided / default crypto-provider
/// </summary>
/// <param name="path"></param>
/// <param name="byteArray"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static bool WriteBinary(string path, byte[] byteArray, ICryptoProvider provider = null) {
try {
if (provider == null) provider = defaultProvider;
byte[] encryptedText = provider.EncryptBinary(byteArray);
File.WriteAllBytes(path, encryptedText);
return true;
}
catch (Exception ex) {
Debug.LogError(ex);
return false;
}
}

/// <summary>
/// Read encrypted file and returns decrypted plain-text using provided / default crypto-provider
/// </summary>
/// <param name="path"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static async Task<string> ReadTextAsync(string path, ICryptoProvider provider = null) {
try {
if (provider == null) provider = defaultProvider;
string encryptedText = await File.ReadAllTextAsync(path);
string decryptedText = provider.DecryptString(encryptedText);
return decryptedText;
}
catch (Exception ex) {
Debug.LogError(ex);
return null;
}
}

/// <summary>
/// Read encrypted binary file and returns decrypted byte array using provided / default crypto-provider
/// </summary>
/// <param name="path"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static async Task<byte[]> ReadBinaryAsync(string path, ICryptoProvider provider = null) {
try {
if (provider == null) provider = defaultProvider;
byte[] encryptedData = await File.ReadAllBytesAsync(path);
byte[] decryptedData = provider.DecryptBinary(encryptedData);
return decryptedData;
}
catch (Exception ex) {
Debug.LogError(ex);
return null;
}
}

/// <summary>
/// Write Text file to path with encrypted plain-text using provided / default crypto-provider
/// </summary>
/// <param name="path"></param>
/// <param name="plainText"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static async Task<bool> WriteTextAsync(string path, string plainText, ICryptoProvider provider = null) {
try {
if (provider == null) provider = defaultProvider;
string encryptedText = provider.EncryptString(plainText);
await File.WriteAllTextAsync(path, encryptedText);
return true;
}
catch (Exception ex) {
Debug.LogError(ex);
return false;
}
}

/// <summary>
/// Write Binary File to path encrypted byte array using provided / default crypto-provider
/// </summary>
/// <param name="path"></param>
/// <param name="byteArray"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static async Task<bool> WriteBinaryAsync(string path, byte[] byteArray, ICryptoProvider provider = null) {
try {
if (provider == null) provider = defaultProvider;
byte[] encryptedText = provider.EncryptBinary(byteArray);
await File.WriteAllBytesAsync(path, encryptedText);
return true;
}
catch (Exception ex) {
Debug.LogError(ex);
return false;
}
}

/// <summary>
/// Read encrypted file and returns decrypted plain-text using provided / default crypto-provider
/// </summary>
/// <param name="path"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static string ReadText(string path, ICryptoProvider provider = null) {
try {
if (provider == null) provider = defaultProvider;
string encryptedText = File.ReadAllText(path);
string decryptedText = provider.DecryptString(encryptedText);
return decryptedText;
}
catch (Exception ex) {
Debug.LogError(ex);
return null;
}
}

/// <summary>
/// Read encrypted binary file and returns decrypted byte array using provided / default crypto-provider
/// </summary>
/// <param name="path"></param>
/// <param name="provider"></param>
/// <returns></returns>
public static byte[] ReadBinary(string path, ICryptoProvider provider = null) {
try {
if (provider == null) provider = defaultProvider;
byte[] encryptedData = File.ReadAllBytes(path);
byte[] decryptedData = provider.DecryptBinary(encryptedData);
return decryptedData;
}
catch (Exception ex) {
Debug.LogError(ex);
return null;
}
}
}
}
3 changes: 3 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/CryptoFile.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/CryptoLibrary.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "CryptoLibrary"
}
7 changes: 7 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/CryptoLibrary.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/CryptoModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DevsDaddy.Shared.CryptoLibrary
{
public enum CryptoModule
{

}
}
3 changes: 3 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/CryptoModule.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/Modules.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/Modules/AES.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions DevsDaddy/Shared/CryptoLibrary/Modules/AES/AESEncryptionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Security.Cryptography;
using System.Text;
using DevsDaddy.Shared.CryptoLibrary.Core;

namespace DevsDaddy.Shared.CryptoLibrary.Modules.AES
{
[System.Serializable]
public class AESEncryptionOptions : IProviderOptions
{
public int bufferKeySize = 32;
public int blockSize = 256;
public int keySize = 256;

public string cryptoKey = "AESPassword";
public Encoding encoding = Encoding.UTF8;

public CipherMode cipherMode = CipherMode.CBC;
public PaddingMode paddingMode = PaddingMode.PKCS7;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2cf931b

Please sign in to comment.