@@ -143,6 +143,8 @@ export default class EZCrypto {
143
143
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
144
144
HASH = async ( algo , data , len ) => {
145
145
146
+ await this . #sleep( 0 ) ;
147
+
146
148
let hash = await this . #crypto. subtle . digest ( algo , new TextEncoder ( ) . encode ( data ) ) ;
147
149
148
150
let ary = new Uint8Array ( hash ) ;
@@ -169,6 +171,94 @@ export default class EZCrypto {
169
171
170
172
171
173
174
+ // //////////////////////////////////////////////////////////////////////////
175
+ // //////////////////////////////////////////////////////////////////////////
176
+ //
177
+ //
178
+ // Function: PASSWORD_ENCRYPT
179
+ // What is this: Dead simple method to encrypt a piece of data with a password
180
+ // that can later be decrypted needing only that password
181
+ //
182
+ // Arguments: password: string; plaintext string of user's password
183
+ // base64data: string; what you want to encrypt
184
+ //
185
+ // Returns: base64 encoded, stringified object containing the AES key used
186
+ // to encrypt the data, and the ciphertext itself
187
+ // Notes:
188
+ //
189
+ //
190
+ // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
191
+ // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
192
+ PASSWORD_ENCRYPT = async ( password , base64data ) => {
193
+
194
+ await this . #sleep( 0 ) ;
195
+
196
+ // let passwordHash = this.HASH("SHA-512", password);
197
+
198
+ for ( let i = 0 ; i < 10_000 ; i ++ ) {
199
+ password = this . HASH ( "SHA-512" , password ) ;
200
+ }
201
+
202
+ let passwordHash = btoa ( password ) ;
203
+
204
+ let aes = await this . AESMakeKey ( true ) ;
205
+
206
+ let output = await this . AESEncrypt ( aes , base64data , passwordHash ) ;
207
+
208
+ return btoa ( JSON . stringify ( { ciphertext : output . ciphertext , aes} ) ) ;
209
+ }
210
+
211
+
212
+
213
+ // //////////////////////////////////////////////////////////////////////////
214
+ // //////////////////////////////////////////////////////////////////////////
215
+ //
216
+ //
217
+ // Function: PASSWORD_DECRYPT
218
+ // What is this: Counterparty to PASSWORD_ENCRYPT. Give it a password, and
219
+ // the encrypted data from PASSWORD_ENCRYPT; it should give you
220
+ // the initial plaintext...
221
+ //
222
+ // Arguments: password: string; plaintext string of user's password
223
+ // base64data: password-data
224
+ //
225
+ // Returns: plaintext
226
+ //
227
+ // Notes:
228
+ //
229
+ //
230
+ // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
231
+ // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
232
+ PASSWORD_DECRYPT = async ( password , base64data ) => {
233
+
234
+ await this . #sleep( 0 ) ;
235
+
236
+ // let passwordHash = this.HASH("SHA-512", password);
237
+
238
+ for ( let i = 0 ; i < 10_000 ; i ++ ) {
239
+ password = this . HASH ( "SHA-512" , password ) ;
240
+ }
241
+
242
+ let passwordHash = btoa ( password ) ;
243
+
244
+ let encryptedDataObject = JSON . parse ( atob ( base64data ) ) ;
245
+
246
+ let aes = await this . AESImportKey ( encryptedDataObject . aes , false ) ;
247
+
248
+ let ciphertext = encryptedDataObject . ciphertext ;
249
+
250
+ let plaintext = await this . AESDecrypt ( aes , passwordHash , ciphertext , true ) ;
251
+
252
+ return plaintext ;
253
+ }
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
172
262
// //////////////////////////////////////////////////////////////////////////
173
263
// //////////////////////////////////////////////////////////////////////////
174
264
//
0 commit comments