@functionland/react-native-fula 1.54.8 → 1.54.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,49 +1,59 @@
1
1
  import Foundation
2
- import CommonCrypto
3
2
  import CryptoSwift
4
3
  import os.log
5
4
 
6
-
7
-
8
- public class Cryptography: NSObject {
9
- public static func encryptMsg(_ message: Array<UInt8>, _ secretKey: Array<UInt8>)
10
- throws -> String {
11
- OSLog.viewCycle.info("ReactNative encryptMsg started for \(message)")
12
- let aes = try! AES(key: secretKey, blockMode: ECB(), padding: .pkcs5)
13
- OSLog.viewCycle.info("ReactNative encryptMsg aes")
14
- let encrypted = try! aes.encrypt(message)
15
- OSLog.viewCycle.info("ReactNative encryptMsg encrypted")
16
- do{
17
- let data = Data(encrypted).base64EncodedString()
18
- OSLog.viewCycle.info("ReactNative encryptMsg: \(data)")
19
- return data
20
- } catch let error {
21
- OSLog.viewCycle.info("ReactNative encryptMsg error: \(error.localizedDescription)")
22
- throw error
5
+ public class Cryptography {
6
+ // AES/GCM Encryption with IV handling
7
+ public static func encryptMsg(_ message: [UInt8], _ secretKey: [UInt8], _ providedIv: [UInt8]? = nil) throws -> String {
8
+ var iv = providedIv ?? [UInt8](repeating: 0, count: 12) // Default IV value
9
+ if iv.isEmpty {
10
+ iv = AES.randomIV(12) // Generate a random IV if not provided or if empty
23
11
  }
12
+
13
+ let gcm = GCM(iv: iv, mode: .combined)
14
+ let aes = try AES(key: secretKey, blockMode: gcm, padding: .noPadding)
15
+
16
+ let encrypted = try aes.encrypt(message)
17
+ let encryptedDataWithIv = Data(iv + encrypted) // Prepend IV to the encrypted data
18
+ return encryptedDataWithIv.base64EncodedString()
24
19
  }
25
20
 
26
- public static func decryptMsg(_ cipherText: String, _ secretKey: Array<UInt8>)
27
- throws -> Array<UInt8> {
28
- let aes = try! AES(key: secretKey, blockMode: ECB(), padding: .pkcs5)
29
- let data = cipherText.fromBase64()!
30
- return try! aes.decrypt(data.bytes)
31
-
21
+ // AES/GCM Decryption with IV extracted from the beginning of the cipherText
22
+ public static func decryptMsg(_ cipherText: String, _ secretKey: [UInt8]) throws -> [UInt8] {
23
+ guard let encryptedDataWithIv = Data(base64Encoded: cipherText) else {
24
+ throw NSError(domain: "DecryptionError", code: -1, userInfo: nil)
25
+ }
26
+ let iv = Array(encryptedDataWithIv.prefix(12)) // Extract IV (first 12 bytes)
27
+ let encryptedData = Array(encryptedDataWithIv.dropFirst(12)) // The rest is the encrypted data
28
+
29
+ let gcm = GCM(iv: iv, mode: .combined)
30
+ let aes = try AES(key: secretKey, blockMode: gcm, padding: .noPadding)
31
+
32
+ let decrypted = try aes.decrypt(encryptedData)
33
+ return decrypted
32
34
  }
33
35
 
34
- public static func generateKey(_ salt: Data)
35
- throws -> Array<UInt8> {
36
- let password: [UInt8] = Array("".utf8)
37
- let salt: [UInt8] = salt.bytes
38
-
39
- //TODO: Generate a key from a salt and an empty password
40
- let key = try PKCS5.PBKDF2(
41
- password: password,
42
- salt: salt,
43
- iterations: 4096,
44
- keyLength: 16, /* AES-128 */
45
- variant: .sha2(SHA2.Variant.sha256)
46
- ).calculate()
47
- return key
36
+ // Key Generation matching Java's logic
37
+ public static func generateKey(_ identity: Data) throws -> Data {
38
+ // Transform identity to base64 string to mimic Java's behavior
39
+ let passwordString = identity.base64EncodedString()
40
+ let passwordData = Array(passwordString.utf8) // Convert to byte array
41
+
42
+ // Use identity itself as salt for simplicity to mimic Java behavior
43
+ let salt = Array(identity)
44
+
45
+ do {
46
+ let key = try PKCS5.PBKDF2(
47
+ password: passwordData,
48
+ salt: salt,
49
+ iterations: 1000, // Adjust the iteration count as needed
50
+ keyLength: 16, // AES-128
51
+ variant: .sha256
52
+ ).calculate()
53
+
54
+ return Data(key)
55
+ } catch {
56
+ throw error // Propagate error
57
+ }
48
58
  }
49
59
  }
package/ios/Fula.mm CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  @interface RCT_EXTERN_MODULE(FulaModule, NSObject)
4
4
 
5
- RCT_EXTERN_METHOD(registerLifecycleListener)
5
+ RCT_EXTERN_METHOD(registerLifecycleListener:(RCTPromiseResolveBlock)resolve
6
+ withRejecter:(RCTPromiseRejectBlock)reject)
6
7
 
7
8
  RCT_EXTERN_METHOD(checkConnection: (nonnull NSNumber *) timeout
8
9
  withResolver:(RCTPromiseResolveBlock)resolve
@@ -189,7 +190,8 @@ RCT_EXTERN_METHOD(getFolderSize:(NSString *)folderPath
189
190
  withResolver:(RCTPromiseResolveBlock)resolve
190
191
  withRejecter:(RCTPromiseRejectBlock)reject)
191
192
 
192
- RCT_EXTERN_METHOD(getDatastoreSize:(RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject)
193
+ RCT_EXTERN_METHOD(getDatastoreSize:(RCTPromiseResolveBlock)resolve
194
+ rejecter:(RCTPromiseRejectBlock)reject)
193
195
 
194
196
  + (BOOL)requiresMainQueueSetup
195
197
  {