Skip to content

Commit 2cf931b

Browse files
authored
Merge pull request #1 from DevsDaddy/develop
Add first release of Crypto Library
2 parents 7bcb909 + d4b7091 commit 2cf931b

File tree

99 files changed

+4002
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+4002
-2
lines changed

DevsDaddy/Shared/CryptoLibrary/Core.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace DevsDaddy.Shared.CryptoLibrary.Core
2+
{
3+
/// <summary>
4+
/// Base Crypto-Provider Interface
5+
/// </summary>
6+
public interface ICryptoProvider
7+
{
8+
/// <summary>
9+
/// Decrypt String Data
10+
/// </summary>
11+
/// <param name="encryptedString"></param>
12+
/// <returns></returns>
13+
string DecryptString(string encryptedString);
14+
15+
/// <summary>
16+
/// Decrypt Binary Data
17+
/// </summary>
18+
/// <param name="encryptedBinary"></param>
19+
/// <returns></returns>
20+
byte[] DecryptBinary(byte[] encryptedBinary);
21+
22+
/// <summary>
23+
/// Encrypt String
24+
/// </summary>
25+
/// <param name="decryptedString"></param>
26+
/// <returns></returns>
27+
string EncryptString(string decryptedString);
28+
29+
/// <summary>
30+
/// Encrypt Binary
31+
/// </summary>
32+
/// <param name="decryptedBinary"></param>
33+
/// <returns></returns>
34+
byte[] EncryptBinary(byte[] decryptedBinary);
35+
}
36+
}

DevsDaddy/Shared/CryptoLibrary/Core/ICryptoProvider.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace DevsDaddy.Shared.CryptoLibrary.Core
2+
{
3+
/// <summary>
4+
/// Provider Options Interface
5+
/// </summary>
6+
public interface IProviderOptions { }
7+
}

DevsDaddy/Shared/CryptoLibrary/Core/IProviderOptions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using DevsDaddy.Shared.CryptoLibrary.Core;
2+
using DevsDaddy.Shared.CryptoLibrary.Modules.AES;
3+
4+
namespace DevsDaddy.Shared.CryptoLibrary
5+
{
6+
public static class CryptoController
7+
{
8+
// Default Provider
9+
private static ICryptoProvider defaultProvider = new AESProvider();
10+
11+
/// <summary>
12+
/// Set Default Crypto Provider
13+
/// </summary>
14+
/// <param name="provider"></param>
15+
public static void SetDefaultProvider(ICryptoProvider provider) {
16+
defaultProvider = provider;
17+
}
18+
19+
/// <summary>
20+
/// Decrypt Encrypted Plain-Text using provided Crypto-provider or Default Crypto-provider
21+
/// </summary>
22+
/// <param name="encryptedString"></param>
23+
/// <param name="provider"></param>
24+
/// <returns></returns>
25+
public static string Decrypt(string encryptedString, ICryptoProvider provider = null) {
26+
if (provider == null) provider = defaultProvider;
27+
return provider.DecryptString(encryptedString);
28+
}
29+
30+
/// <summary>
31+
/// Decrypt encrypted byte array using provided Crypto-provider or Default Crypto-provider
32+
/// </summary>
33+
/// <param name="encryptedArray"></param>
34+
/// <param name="provider"></param>
35+
/// <returns></returns>
36+
public static byte[] Decrypt(byte[] encryptedArray, ICryptoProvider provider) {
37+
if (provider == null) provider = defaultProvider;
38+
return provider.DecryptBinary(encryptedArray);
39+
}
40+
41+
/// <summary>
42+
/// Encrypt plain-text using provided Crypto-provider or Default Crypto-provider
43+
/// </summary>
44+
/// <param name="plainText"></param>
45+
/// <param name="provider"></param>
46+
/// <returns></returns>
47+
public static string Encrypt(string plainText, ICryptoProvider provider = null) {
48+
if (provider == null) provider = defaultProvider;
49+
return provider.EncryptString(plainText);
50+
}
51+
52+
/// <summary>
53+
/// Encrypt binary array using provided Crypto-provider or Default Crypto-provider
54+
/// </summary>
55+
/// <param name="decryptedArray"></param>
56+
/// <param name="provider"></param>
57+
/// <returns></returns>
58+
public static byte[] Encrypt(byte[] decryptedArray, ICryptoProvider provider = null) {
59+
if (provider == null) provider = defaultProvider;
60+
return provider.EncryptBinary(decryptedArray);
61+
}
62+
63+
public static void Test() {
64+
CryptoFile.SetDefaultProvider(new AESProvider(new AESEncryptionOptions {
65+
cryptoKey = "key"
66+
}));
67+
68+
string decryptedText = CryptoFile.ReadText("path_to_encrypted_file");
69+
bool writtenFile = CryptoFile.WriteText("path_to_encrypted_file", decryptedText);
70+
}
71+
}
72+
}

DevsDaddy/Shared/CryptoLibrary/CryptoController.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using DevsDaddy.Shared.CryptoLibrary.Core;
6+
using DevsDaddy.Shared.CryptoLibrary.Modules.AES;
7+
using UnityEngine;
8+
9+
namespace DevsDaddy.Shared.CryptoLibrary
10+
{
11+
public static class CryptoFile
12+
{
13+
// Default Provider
14+
private static ICryptoProvider defaultProvider = new AESProvider();
15+
16+
/// <summary>
17+
/// Set Default Crypto Provider
18+
/// </summary>
19+
/// <param name="provider"></param>
20+
public static void SetDefaultProvider(ICryptoProvider provider) {
21+
defaultProvider = provider;
22+
}
23+
24+
/// <summary>
25+
/// Write Text file to path with encrypted plain-text using provided / default crypto-provider
26+
/// </summary>
27+
/// <param name="path"></param>
28+
/// <param name="plainText"></param>
29+
/// <param name="provider"></param>
30+
/// <returns></returns>
31+
public static bool WriteText(string path, string plainText, ICryptoProvider provider = null) {
32+
try {
33+
if (provider == null) provider = defaultProvider;
34+
string encryptedText = provider.EncryptString(plainText);
35+
File.WriteAllText(path, encryptedText);
36+
return true;
37+
}
38+
catch (Exception ex) {
39+
Debug.LogError(ex);
40+
return false;
41+
}
42+
}
43+
44+
/// <summary>
45+
/// Write Binary File to path encrypted byte array using provided / default crypto-provider
46+
/// </summary>
47+
/// <param name="path"></param>
48+
/// <param name="byteArray"></param>
49+
/// <param name="provider"></param>
50+
/// <returns></returns>
51+
public static bool WriteBinary(string path, byte[] byteArray, ICryptoProvider provider = null) {
52+
try {
53+
if (provider == null) provider = defaultProvider;
54+
byte[] encryptedText = provider.EncryptBinary(byteArray);
55+
File.WriteAllBytes(path, encryptedText);
56+
return true;
57+
}
58+
catch (Exception ex) {
59+
Debug.LogError(ex);
60+
return false;
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Read encrypted file and returns decrypted plain-text using provided / default crypto-provider
66+
/// </summary>
67+
/// <param name="path"></param>
68+
/// <param name="provider"></param>
69+
/// <returns></returns>
70+
public static async Task<string> ReadTextAsync(string path, ICryptoProvider provider = null) {
71+
try {
72+
if (provider == null) provider = defaultProvider;
73+
string encryptedText = await File.ReadAllTextAsync(path);
74+
string decryptedText = provider.DecryptString(encryptedText);
75+
return decryptedText;
76+
}
77+
catch (Exception ex) {
78+
Debug.LogError(ex);
79+
return null;
80+
}
81+
}
82+
83+
/// <summary>
84+
/// Read encrypted binary file and returns decrypted byte array using provided / default crypto-provider
85+
/// </summary>
86+
/// <param name="path"></param>
87+
/// <param name="provider"></param>
88+
/// <returns></returns>
89+
public static async Task<byte[]> ReadBinaryAsync(string path, ICryptoProvider provider = null) {
90+
try {
91+
if (provider == null) provider = defaultProvider;
92+
byte[] encryptedData = await File.ReadAllBytesAsync(path);
93+
byte[] decryptedData = provider.DecryptBinary(encryptedData);
94+
return decryptedData;
95+
}
96+
catch (Exception ex) {
97+
Debug.LogError(ex);
98+
return null;
99+
}
100+
}
101+
102+
/// <summary>
103+
/// Write Text file to path with encrypted plain-text using provided / default crypto-provider
104+
/// </summary>
105+
/// <param name="path"></param>
106+
/// <param name="plainText"></param>
107+
/// <param name="provider"></param>
108+
/// <returns></returns>
109+
public static async Task<bool> WriteTextAsync(string path, string plainText, ICryptoProvider provider = null) {
110+
try {
111+
if (provider == null) provider = defaultProvider;
112+
string encryptedText = provider.EncryptString(plainText);
113+
await File.WriteAllTextAsync(path, encryptedText);
114+
return true;
115+
}
116+
catch (Exception ex) {
117+
Debug.LogError(ex);
118+
return false;
119+
}
120+
}
121+
122+
/// <summary>
123+
/// Write Binary File to path encrypted byte array using provided / default crypto-provider
124+
/// </summary>
125+
/// <param name="path"></param>
126+
/// <param name="byteArray"></param>
127+
/// <param name="provider"></param>
128+
/// <returns></returns>
129+
public static async Task<bool> WriteBinaryAsync(string path, byte[] byteArray, ICryptoProvider provider = null) {
130+
try {
131+
if (provider == null) provider = defaultProvider;
132+
byte[] encryptedText = provider.EncryptBinary(byteArray);
133+
await File.WriteAllBytesAsync(path, encryptedText);
134+
return true;
135+
}
136+
catch (Exception ex) {
137+
Debug.LogError(ex);
138+
return false;
139+
}
140+
}
141+
142+
/// <summary>
143+
/// Read encrypted file and returns decrypted plain-text using provided / default crypto-provider
144+
/// </summary>
145+
/// <param name="path"></param>
146+
/// <param name="provider"></param>
147+
/// <returns></returns>
148+
public static string ReadText(string path, ICryptoProvider provider = null) {
149+
try {
150+
if (provider == null) provider = defaultProvider;
151+
string encryptedText = File.ReadAllText(path);
152+
string decryptedText = provider.DecryptString(encryptedText);
153+
return decryptedText;
154+
}
155+
catch (Exception ex) {
156+
Debug.LogError(ex);
157+
return null;
158+
}
159+
}
160+
161+
/// <summary>
162+
/// Read encrypted binary file and returns decrypted byte array using provided / default crypto-provider
163+
/// </summary>
164+
/// <param name="path"></param>
165+
/// <param name="provider"></param>
166+
/// <returns></returns>
167+
public static byte[] ReadBinary(string path, ICryptoProvider provider = null) {
168+
try {
169+
if (provider == null) provider = defaultProvider;
170+
byte[] encryptedData = File.ReadAllBytes(path);
171+
byte[] decryptedData = provider.DecryptBinary(encryptedData);
172+
return decryptedData;
173+
}
174+
catch (Exception ex) {
175+
Debug.LogError(ex);
176+
return null;
177+
}
178+
}
179+
}
180+
}

DevsDaddy/Shared/CryptoLibrary/CryptoFile.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "CryptoLibrary"
3+
}

0 commit comments

Comments
 (0)