Skip to content

Commit

Permalink
Removed the need for .enc and -new
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolmello committed May 15, 2024
1 parent f6d677a commit ab0123a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 51 deletions.
3 changes: 1 addition & 2 deletions app/lib/methods/handleMediaDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const getFilePath = ({
const folderPath = getFolderPath(urlToCache);
const urlWithoutQueryString = urlToCache.split('?')[0];
const filename = sanitizeFileName(getFilename({ type, mimeType, url: urlWithoutQueryString }));
const filePath = `${folderPath}${filename}${encrypted ? '.enc' : ''}`;
const filePath = `${folderPath}${filename}`;
return filePath;
};

Expand Down Expand Up @@ -225,7 +225,6 @@ export function downloadMediaFile({
}

const decryptedFile = await decryptAESCTR(result.uri, encryption.key.k, encryption.iv);

if (decryptedFile) {
return resolve(decryptedFile);
}
Expand Down
101 changes: 52 additions & 49 deletions patches/react-native-simple-crypto+0.5.1.patch
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ index 0000000..0d259dd
+o/classes
diff --git a/node_modules/react-native-simple-crypto/android/build/.transforms/bdbefe898e24b23c7477c1cbc3116307/transformed/classes/classes.dex b/node_modules/react-native-simple-crypto/android/build/.transforms/bdbefe898e24b23c7477c1cbc3116307/transformed/classes/classes.dex
new file mode 100644
index 0000000..b4f833b
index 0000000..739d8fb
Binary files /dev/null and b/node_modules/react-native-simple-crypto/android/build/.transforms/bdbefe898e24b23c7477c1cbc3116307/transformed/classes/classes.dex differ
diff --git a/node_modules/react-native-simple-crypto/android/build/.transforms/c2ec0bcb49ee954746918a6a074d37b1/results.bin b/node_modules/react-native-simple-crypto/android/build/.transforms/c2ec0bcb49ee954746918a6a074d37b1/results.bin
new file mode 100644
Expand Down Expand Up @@ -162,7 +162,7 @@ index 0000000..9e26dfe
\ No newline at end of file
diff --git a/node_modules/react-native-simple-crypto/android/build/intermediates/compile_library_classes_jar/debug/classes.jar b/node_modules/react-native-simple-crypto/android/build/intermediates/compile_library_classes_jar/debug/classes.jar
new file mode 100644
index 0000000..3a4db3a
index 0000000..5f4686f
Binary files /dev/null and b/node_modules/react-native-simple-crypto/android/build/intermediates/compile_library_classes_jar/debug/classes.jar differ
diff --git a/node_modules/react-native-simple-crypto/android/build/intermediates/compile_r_class_jar/debug/R.jar b/node_modules/react-native-simple-crypto/android/build/intermediates/compile_r_class_jar/debug/R.jar
new file mode 100644
Expand Down Expand Up @@ -2083,7 +2083,7 @@ index 0000000..99b9873
Binary files /dev/null and b/node_modules/react-native-simple-crypto/android/build/intermediates/javac/debug/classes/com/pedrouid/crypto/BuildConfig.class differ
diff --git a/node_modules/react-native-simple-crypto/android/build/intermediates/javac/debug/classes/com/pedrouid/crypto/RCTAes.class b/node_modules/react-native-simple-crypto/android/build/intermediates/javac/debug/classes/com/pedrouid/crypto/RCTAes.class
new file mode 100644
index 0000000..c3a796f
index 0000000..d0b7d47
Binary files /dev/null and b/node_modules/react-native-simple-crypto/android/build/intermediates/javac/debug/classes/com/pedrouid/crypto/RCTAes.class differ
diff --git a/node_modules/react-native-simple-crypto/android/build/intermediates/javac/debug/classes/com/pedrouid/crypto/RCTCryptoPackage.class b/node_modules/react-native-simple-crypto/android/build/intermediates/javac/debug/classes/com/pedrouid/crypto/RCTCryptoPackage.class
new file mode 100644
Expand Down Expand Up @@ -2155,7 +2155,7 @@ index 0000000..34450ae
Binary files /dev/null and b/node_modules/react-native-simple-crypto/android/build/intermediates/javac/debug/classes/com/pedrouid/crypto/RandomBytesModule.class differ
diff --git a/node_modules/react-native-simple-crypto/android/build/intermediates/javac/debug/classes/com/pedrouid/crypto/Util.class b/node_modules/react-native-simple-crypto/android/build/intermediates/javac/debug/classes/com/pedrouid/crypto/Util.class
new file mode 100644
index 0000000..4837fc4
index 0000000..456051a
Binary files /dev/null and b/node_modules/react-native-simple-crypto/android/build/intermediates/javac/debug/classes/com/pedrouid/crypto/Util.class differ
diff --git a/node_modules/react-native-simple-crypto/android/build/intermediates/local_only_symbol_list/debug/R-def.txt b/node_modules/react-native-simple-crypto/android/build/intermediates/local_only_symbol_list/debug/R-def.txt
new file mode 100644
Expand Down Expand Up @@ -2293,7 +2293,7 @@ new file mode 100644
index 0000000..e69de29
diff --git a/node_modules/react-native-simple-crypto/android/build/intermediates/runtime_library_classes_jar/debug/classes.jar b/node_modules/react-native-simple-crypto/android/build/intermediates/runtime_library_classes_jar/debug/classes.jar
new file mode 100644
index 0000000..5429be9
index 0000000..ebffdbe
Binary files /dev/null and b/node_modules/react-native-simple-crypto/android/build/intermediates/runtime_library_classes_jar/debug/classes.jar differ
diff --git a/node_modules/react-native-simple-crypto/android/build/intermediates/symbol_list_with_package_name/debug/package-aware-r.txt b/node_modules/react-native-simple-crypto/android/build/intermediates/symbol_list_with_package_name/debug/package-aware-r.txt
new file mode 100644
Expand Down Expand Up @@ -3714,42 +3714,42 @@ index 0000000..8fc244a
+ INJECTED from /Users/diegomello/Development/Work/Rocket.Chat.ReactNative/node_modules/react-native-simple-crypto/android/src/main/AndroidManifest.xml
diff --git a/node_modules/react-native-simple-crypto/android/build/tmp/compileDebugJavaWithJavac/previous-compilation-data.bin b/node_modules/react-native-simple-crypto/android/build/tmp/compileDebugJavaWithJavac/previous-compilation-data.bin
new file mode 100644
index 0000000..9aa8252
index 0000000..8e9b0f8
Binary files /dev/null and b/node_modules/react-native-simple-crypto/android/build/tmp/compileDebugJavaWithJavac/previous-compilation-data.bin differ
diff --git a/node_modules/react-native-simple-crypto/android/src/main/java/com/pedrouid/crypto/RCTAes.java b/node_modules/react-native-simple-crypto/android/src/main/java/com/pedrouid/crypto/RCTAes.java
index 2b52abe..0ef37a6 100644
index 2b52abe..cccfdec 100644
--- a/node_modules/react-native-simple-crypto/android/src/main/java/com/pedrouid/crypto/RCTAes.java
+++ b/node_modules/react-native-simple-crypto/android/src/main/java/com/pedrouid/crypto/RCTAes.java
@@ -1,55 +1,31 @@
@@ -1,55 +1,32 @@
package com.pedrouid.crypto;

-import android.widget.Toast;
-
-import java.io.IOException;
-import java.security.SecureRandom;
-import java.util.HashMap;
-import java.util.Map;
+import android.util.Base64;

-import java.util.UUID;
+
+import com.facebook.react.bridge.Promise;
+import com.facebook.react.bridge.ReactApplicationContext;
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
+import com.facebook.react.bridge.ReactMethod;

+import org.spongycastle.util.encoders.Hex;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.security.SecureRandom;
-import java.util.HashMap;
-import java.util.Map;
-
import java.util.UUID;
-
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.security.spec.InvalidKeySpecException;
-import java.security.InvalidKeyException;
+import org.spongycastle.util.encoders.Hex;

-
-import java.nio.charset.StandardCharsets;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.security.SecureRandom;
+import java.util.UUID;

-
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
-import javax.crypto.spec.SecretKeySpec;
Expand Down Expand Up @@ -3780,6 +3780,7 @@ index 2b52abe..0ef37a6 100644
-import com.facebook.react.bridge.ReactMethod;
-import com.facebook.react.bridge.Callback;
+import javax.crypto.spec.SecretKeySpec;
+import java.nio.channels.FileChannel;

public class RCTAes extends ReactContextBaseJavaModule {

Expand All @@ -3790,7 +3791,7 @@ index 2b52abe..0ef37a6 100644

public RCTAes(ReactApplicationContext reactContext) {
super(reactContext);
@@ -80,6 +56,26 @@ public class RCTAes extends ReactContextBaseJavaModule {
@@ -80,6 +57,26 @@ public class RCTAes extends ReactContextBaseJavaModule {
}
}

Expand All @@ -3817,7 +3818,7 @@ index 2b52abe..0ef37a6 100644
@ReactMethod
public void randomUuid(Promise promise) {
try {
@@ -105,6 +101,12 @@ public class RCTAes extends ReactContextBaseJavaModule {
@@ -105,6 +102,12 @@ public class RCTAes extends ReactContextBaseJavaModule {

final static IvParameterSpec emptyIvSpec = new IvParameterSpec(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});

Expand All @@ -3830,13 +3831,12 @@ index 2b52abe..0ef37a6 100644
public static String encrypt(String textBase64, String hexKey, String hexIv) throws Exception {
if (textBase64 == null || textBase64.length() == 0) {
return null;
@@ -133,4 +135,51 @@ public class RCTAes extends ReactContextBaseJavaModule {
@@ -133,4 +136,57 @@ public class RCTAes extends ReactContextBaseJavaModule {
return Base64.encodeToString(decrypted, Base64.NO_WRAP);
}

+ public static String processFile(String inputFile, String base64UrlKey, String base64Iv, String mode, String fileSuffix) throws Exception {
+
+ // Decode the key and IV
+ public static String processFile(String inputFile, String base64UrlKey, String base64Iv, String mode) throws Exception {
+ // Decode the key and IV using URL-safe and no-wrap flags
+ byte[] key = Base64.decode(base64UrlKey, Base64.URL_SAFE | Base64.NO_WRAP);
+ byte[] iv = Base64.decode(base64Iv, Base64.NO_WRAP);
+ SecretKey secretKey = new SecretKeySpec(key, "AES");
Expand All @@ -3846,16 +3846,17 @@ index 2b52abe..0ef37a6 100644
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
+ cipher.init(mode.equals("encrypt") ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
+
+ // Setup file input and output
+ inputFile = Util.normalizeFilePath(inputFile);
+ File inputFileObj = new File(inputFile);
+ FileInputStream fis = new FileInputStream(inputFileObj);
+ String outputFile = Util.addFileScheme(inputFile + fileSuffix);
+ FileOutputStream fos = new FileOutputStream(new File(Util.normalizeFilePath(outputFile)));
+ // Normalize file paths
+ String normalizedInputFilePath = Util.normalizeFilePath(inputFile);
+ File inputFileObj = new File(normalizedInputFilePath);
+ File outputFileObj = new File(normalizedInputFilePath + ".tmp");
+
+ try {
+ byte[] buffer = new byte[4096];
+ // File streams setup
+ try (FileInputStream fis = new FileInputStream(inputFileObj);
+ FileOutputStream fos = new FileOutputStream(outputFileObj)) {
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int numBytesRead;
+
+ while ((numBytesRead = fis.read(buffer)) != -1) {
+ byte[] output = cipher.update(buffer, 0, numBytesRead);
+ if (output != null) {
Expand All @@ -3866,27 +3867,33 @@ index 2b52abe..0ef37a6 100644
+ if (finalBytes != null) {
+ fos.write(finalBytes);
+ }
+ } finally {
+ fis.close();
+ fos.close();
+ }
+
+ return outputFile;
+ // Replace original file with the processed file
+ if (inputFileObj.delete()) {
+ if (!outputFileObj.renameTo(inputFileObj)) {
+ throw new IOException("Failed to rename output file back to original.");
+ }
+ } else {
+ throw new IOException("Failed to delete original file.");
+ }
+
+ return inputFile;
+ }
+
+ public static String encryptFile(String inputFile, String base64UrlKey, String base64Iv) throws Exception {
+ return processFile(inputFile, base64UrlKey, base64Iv, "encrypt", ".enc");
+ return processFile(inputFile, base64UrlKey, base64Iv, "encrypt");
+ }
+
+ public static String decryptFile(String inputFile, String base64UrlKey, String base64Iv) throws Exception {
+ return processFile(inputFile, base64UrlKey, base64Iv, "decrypt", "-new.jpg");
+ return processFile(inputFile, base64UrlKey, base64Iv, "decrypt");
+ }
}
diff --git a/node_modules/react-native-simple-crypto/android/src/main/java/com/pedrouid/crypto/Util.java b/node_modules/react-native-simple-crypto/android/src/main/java/com/pedrouid/crypto/Util.java
index 25179a9..0ca7b29 100644
index 25179a9..2ff673b 100644
--- a/node_modules/react-native-simple-crypto/android/src/main/java/com/pedrouid/crypto/Util.java
+++ b/node_modules/react-native-simple-crypto/android/src/main/java/com/pedrouid/crypto/Util.java
@@ -11,4 +11,22 @@ public class Util {
@@ -11,4 +11,18 @@ public class Util {
}
return new String(hexChars);
}
Expand All @@ -3903,10 +3910,6 @@ index 25179a9..0ca7b29 100644
+
+ public static String normalizeFilePath(String filePath) {
+ return filePath.startsWith("file://") ? filePath.substring(7) : filePath;
+ }
+
+ public static String addFileScheme(String filePath) {
+ return "file://" + filePath;
+ }
}
diff --git a/node_modules/react-native-simple-crypto/index.d.ts b/node_modules/react-native-simple-crypto/index.d.ts
Expand Down

0 comments on commit ab0123a

Please sign in to comment.