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+ }
0 commit comments