-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Initial Crypto Modules and Hashing, CryptoController and CryptoFile classes
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 { } | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "CryptoLibrary" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace DevsDaddy.Shared.CryptoLibrary | ||
{ | ||
public enum CryptoModule | ||
{ | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.