@pmishra0/react-native-aes-gcm 0.1.0

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.
Files changed (46) hide show
  1. package/AesGcm.podspec +34 -0
  2. package/LICENSE +20 -0
  3. package/README.md +33 -0
  4. package/android/build.gradle +100 -0
  5. package/android/generated/java/com/aesgcm/NativeAesGcmSpec.java +42 -0
  6. package/android/generated/jni/CMakeLists.txt +28 -0
  7. package/android/generated/jni/RNAesGcmSpec-generated.cpp +38 -0
  8. package/android/generated/jni/RNAesGcmSpec.h +31 -0
  9. package/android/generated/jni/react/renderer/components/RNAesGcmSpec/RNAesGcmSpecJSI.h +51 -0
  10. package/android/gradle.properties +5 -0
  11. package/android/src/main/AndroidManifest.xml +3 -0
  12. package/android/src/main/AndroidManifestNew.xml +2 -0
  13. package/android/src/main/java/com/aesgcm/AesGcmModule.kt +116 -0
  14. package/android/src/main/java/com/aesgcm/AesGcmPackage.kt +33 -0
  15. package/ios/AesGcm.h +6 -0
  16. package/ios/AesGcm.mm +66 -0
  17. package/ios/EncryptionManager.swift +138 -0
  18. package/ios/generated/Package.swift +59 -0
  19. package/ios/generated/ReactAppDependencyProvider/RCTAppDependencyProvider.h +25 -0
  20. package/ios/generated/ReactAppDependencyProvider/RCTAppDependencyProvider.mm +40 -0
  21. package/ios/generated/ReactAppDependencyProvider/ReactAppDependencyProvider.podspec +34 -0
  22. package/ios/generated/ReactCodegen/RCTModuleProviders.h +16 -0
  23. package/ios/generated/ReactCodegen/RCTModuleProviders.mm +51 -0
  24. package/ios/generated/ReactCodegen/RCTModulesConformingToProtocolsProvider.h +18 -0
  25. package/ios/generated/ReactCodegen/RCTModulesConformingToProtocolsProvider.mm +54 -0
  26. package/ios/generated/ReactCodegen/RCTThirdPartyComponentsProvider.h +16 -0
  27. package/ios/generated/ReactCodegen/RCTThirdPartyComponentsProvider.mm +30 -0
  28. package/ios/generated/ReactCodegen/RCTUnstableModulesRequiringMainQueueSetupProvider.h +14 -0
  29. package/ios/generated/ReactCodegen/RCTUnstableModulesRequiringMainQueueSetupProvider.mm +19 -0
  30. package/ios/generated/ReactCodegen/RNAesGcmSpec/RNAesGcmSpec-generated.mm +46 -0
  31. package/ios/generated/ReactCodegen/RNAesGcmSpec/RNAesGcmSpec.h +71 -0
  32. package/ios/generated/ReactCodegen/RNAesGcmSpecJSI.h +51 -0
  33. package/ios/generated/ReactCodegen/ReactCodegen.podspec +110 -0
  34. package/lib/module/NativeAesGcm.ts +17 -0
  35. package/lib/module/index.js +10 -0
  36. package/lib/module/index.js.map +1 -0
  37. package/lib/module/package.json +1 -0
  38. package/lib/typescript/package.json +1 -0
  39. package/lib/typescript/src/NativeAesGcm.d.ts +8 -0
  40. package/lib/typescript/src/NativeAesGcm.d.ts.map +1 -0
  41. package/lib/typescript/src/index.d.ts +3 -0
  42. package/lib/typescript/src/index.d.ts.map +1 -0
  43. package/package.json +171 -0
  44. package/react-native.config.js +12 -0
  45. package/src/NativeAesGcm.ts +17 -0
  46. package/src/index.tsx +17 -0
package/AesGcm.podspec ADDED
@@ -0,0 +1,34 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "AesGcm"
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => min_ios_version_supported }
14
+ s.source = { :git => "https://github.com/prashantkmishra/rn-aes-gcm-crypto.git.git", :tag => "#{s.version}" }
15
+
16
+ s.source_files = "ios/**/*.{h,m,mm,swift}"
17
+ s.exclude_files = [
18
+ "ios/**/RCTAppDependencyProvider.*",
19
+ "ios/**/RCTModuleProviders.*",
20
+ "ios/**/RCTThirdPartyComponentsProvider.*",
21
+ "ios/**/RCTModulesConformingToProtocolsProvider.*",
22
+ "ios/**/RCTUnstableModulesRequiringMainQueueSetupProvider.*"
23
+ ]
24
+ s.private_header_files = "ios/**/*.h"
25
+ s.dependency "CryptoSwift"
26
+
27
+ # Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
28
+ # See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
29
+ if respond_to?(:install_modules_dependencies, true)
30
+ install_modules_dependencies(s)
31
+ else
32
+ s.dependency "React-Core"
33
+ end
34
+ end
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Prashant Mishra
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # react-native-aes-gcm
2
+
3
+ AES-GCM encryption/decryption for React Native
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install react-native-aes-gcm
9
+ ```
10
+
11
+ ## Usage
12
+
13
+
14
+ ```js
15
+ import { multiply } from 'react-native-aes-gcm';
16
+
17
+ // ...
18
+
19
+ const result = multiply(3, 7);
20
+ ```
21
+
22
+
23
+ ## Contributing
24
+
25
+ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
26
+
27
+ ## License
28
+
29
+ MIT
30
+
31
+ ---
32
+
33
+ Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
@@ -0,0 +1,100 @@
1
+ buildscript {
2
+ ext.getExtOrDefault = {name ->
3
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['AesGcm_' + name]
4
+ }
5
+
6
+ repositories {
7
+ google()
8
+ mavenCentral()
9
+ }
10
+
11
+ dependencies {
12
+ classpath "com.android.tools.build:gradle:8.7.2"
13
+ // noinspection DifferentKotlinGradleVersion
14
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
15
+ }
16
+ }
17
+
18
+
19
+ apply plugin: "com.android.library"
20
+ apply plugin: "kotlin-android"
21
+
22
+ apply plugin: "com.facebook.react"
23
+
24
+ def getExtOrIntegerDefault(name) {
25
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["AesGcm_" + name]).toInteger()
26
+ }
27
+
28
+ def supportsNamespace() {
29
+ def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
30
+ def major = parsed[0].toInteger()
31
+ def minor = parsed[1].toInteger()
32
+
33
+ // Namespace support was added in 7.3.0
34
+ return (major == 7 && minor >= 3) || major >= 8
35
+ }
36
+
37
+ android {
38
+ if (supportsNamespace()) {
39
+ namespace "com.aesgcm"
40
+
41
+ sourceSets {
42
+ main {
43
+ manifest.srcFile "src/main/AndroidManifestNew.xml"
44
+ }
45
+ }
46
+ }
47
+
48
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
49
+
50
+ defaultConfig {
51
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
52
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
53
+ }
54
+
55
+ buildFeatures {
56
+ buildConfig true
57
+ }
58
+
59
+ buildTypes {
60
+ release {
61
+ minifyEnabled false
62
+ }
63
+ }
64
+
65
+ lintOptions {
66
+ disable "GradleCompatible"
67
+ }
68
+
69
+ compileOptions {
70
+ sourceCompatibility JavaVersion.VERSION_1_8
71
+ targetCompatibility JavaVersion.VERSION_1_8
72
+ }
73
+
74
+ sourceSets {
75
+ main {
76
+ java.srcDirs += [
77
+ "generated/java",
78
+ "generated/jni"
79
+ ]
80
+ }
81
+ }
82
+ }
83
+
84
+ repositories {
85
+ mavenCentral()
86
+ google()
87
+ }
88
+
89
+ def kotlin_version = getExtOrDefault("kotlinVersion")
90
+
91
+ dependencies {
92
+ implementation "com.facebook.react:react-android"
93
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
94
+ }
95
+
96
+ react {
97
+ jsRootDir = file("../src/")
98
+ libraryName = "AesGcm"
99
+ codegenJavaPackageName = "com.aesgcm"
100
+ }
@@ -0,0 +1,42 @@
1
+
2
+ /**
3
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
4
+ *
5
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
6
+ * once the code is regenerated.
7
+ *
8
+ * @generated by codegen project: GenerateModuleJavaSpec.js
9
+ *
10
+ * @nolint
11
+ */
12
+
13
+ package com.aesgcm;
14
+
15
+ import com.facebook.proguard.annotations.DoNotStrip;
16
+ import com.facebook.react.bridge.Promise;
17
+ import com.facebook.react.bridge.ReactApplicationContext;
18
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
19
+ import com.facebook.react.bridge.ReactMethod;
20
+ import com.facebook.react.turbomodule.core.interfaces.TurboModule;
21
+ import javax.annotation.Nonnull;
22
+
23
+ public abstract class NativeAesGcmSpec extends ReactContextBaseJavaModule implements TurboModule {
24
+ public static final String NAME = "AesGcm";
25
+
26
+ public NativeAesGcmSpec(ReactApplicationContext reactContext) {
27
+ super(reactContext);
28
+ }
29
+
30
+ @Override
31
+ public @Nonnull String getName() {
32
+ return NAME;
33
+ }
34
+
35
+ @ReactMethod
36
+ @DoNotStrip
37
+ public abstract void encrypt(String plainText, String key, double iterationCount, Promise promise);
38
+
39
+ @ReactMethod
40
+ @DoNotStrip
41
+ public abstract void decrypt(String encryptedText, String key, double iterationCount, Promise promise);
42
+ }
@@ -0,0 +1,28 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ cmake_minimum_required(VERSION 3.13)
7
+ set(CMAKE_VERBOSE_MAKEFILE on)
8
+
9
+ file(GLOB react_codegen_SRCS CONFIGURE_DEPENDS *.cpp react/renderer/components/RNAesGcmSpec/*.cpp)
10
+
11
+ add_library(
12
+ react_codegen_RNAesGcmSpec
13
+ OBJECT
14
+ ${react_codegen_SRCS}
15
+ )
16
+
17
+ target_include_directories(react_codegen_RNAesGcmSpec PUBLIC . react/renderer/components/RNAesGcmSpec)
18
+
19
+ target_link_libraries(
20
+ react_codegen_RNAesGcmSpec
21
+ fbjni
22
+ jsi
23
+ # We need to link different libraries based on whether we are building rncore or not, that's necessary
24
+ # because we want to break a circular dependency between react_codegen_rncore and reactnative
25
+ reactnative
26
+ )
27
+
28
+ target_compile_reactnative_options(react_codegen_RNAesGcmSpec PRIVATE)
@@ -0,0 +1,38 @@
1
+
2
+ /**
3
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
4
+ *
5
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
6
+ * once the code is regenerated.
7
+ *
8
+ * @generated by codegen project: GenerateModuleJniCpp.js
9
+ */
10
+
11
+ #include "RNAesGcmSpec.h"
12
+
13
+ namespace facebook::react {
14
+
15
+ static facebook::jsi::Value __hostFunction_NativeAesGcmSpecJSI_encrypt(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
16
+ static jmethodID cachedMethodId = nullptr;
17
+ return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, PromiseKind, "encrypt", "(Ljava/lang/String;Ljava/lang/String;DLcom/facebook/react/bridge/Promise;)V", args, count, cachedMethodId);
18
+ }
19
+
20
+ static facebook::jsi::Value __hostFunction_NativeAesGcmSpecJSI_decrypt(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
21
+ static jmethodID cachedMethodId = nullptr;
22
+ return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, PromiseKind, "decrypt", "(Ljava/lang/String;Ljava/lang/String;DLcom/facebook/react/bridge/Promise;)V", args, count, cachedMethodId);
23
+ }
24
+
25
+ NativeAesGcmSpecJSI::NativeAesGcmSpecJSI(const JavaTurboModule::InitParams &params)
26
+ : JavaTurboModule(params) {
27
+ methodMap_["encrypt"] = MethodMetadata {3, __hostFunction_NativeAesGcmSpecJSI_encrypt};
28
+ methodMap_["decrypt"] = MethodMetadata {3, __hostFunction_NativeAesGcmSpecJSI_decrypt};
29
+ }
30
+
31
+ std::shared_ptr<TurboModule> RNAesGcmSpec_ModuleProvider(const std::string &moduleName, const JavaTurboModule::InitParams &params) {
32
+ if (moduleName == "AesGcm") {
33
+ return std::make_shared<NativeAesGcmSpecJSI>(params);
34
+ }
35
+ return nullptr;
36
+ }
37
+
38
+ } // namespace facebook::react
@@ -0,0 +1,31 @@
1
+
2
+ /**
3
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
4
+ *
5
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
6
+ * once the code is regenerated.
7
+ *
8
+ * @generated by codegen project: GenerateModuleJniH.js
9
+ */
10
+
11
+ #pragma once
12
+
13
+ #include <ReactCommon/JavaTurboModule.h>
14
+ #include <ReactCommon/TurboModule.h>
15
+ #include <jsi/jsi.h>
16
+
17
+ namespace facebook::react {
18
+
19
+ /**
20
+ * JNI C++ class for module 'NativeAesGcm'
21
+ */
22
+ class JSI_EXPORT NativeAesGcmSpecJSI : public JavaTurboModule {
23
+ public:
24
+ NativeAesGcmSpecJSI(const JavaTurboModule::InitParams &params);
25
+ };
26
+
27
+
28
+ JSI_EXPORT
29
+ std::shared_ptr<TurboModule> RNAesGcmSpec_ModuleProvider(const std::string &moduleName, const JavaTurboModule::InitParams &params);
30
+
31
+ } // namespace facebook::react
@@ -0,0 +1,51 @@
1
+ /**
2
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
3
+ *
4
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
5
+ * once the code is regenerated.
6
+ *
7
+ * @generated by codegen project: GenerateModuleH.js
8
+ */
9
+
10
+ #pragma once
11
+
12
+ #include <ReactCommon/TurboModule.h>
13
+ #include <react/bridging/Bridging.h>
14
+
15
+ namespace facebook::react {
16
+
17
+
18
+ template <typename T>
19
+ class JSI_EXPORT NativeAesGcmCxxSpec : public TurboModule {
20
+ public:
21
+ static constexpr std::string_view kModuleName = "AesGcm";
22
+
23
+ protected:
24
+ NativeAesGcmCxxSpec(std::shared_ptr<CallInvoker> jsInvoker) : TurboModule(std::string{NativeAesGcmCxxSpec::kModuleName}, jsInvoker) {
25
+ methodMap_["encrypt"] = MethodMetadata {.argCount = 3, .invoker = __encrypt};
26
+ methodMap_["decrypt"] = MethodMetadata {.argCount = 3, .invoker = __decrypt};
27
+ }
28
+
29
+ private:
30
+ static jsi::Value __encrypt(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
31
+ static_assert(
32
+ bridging::getParameterCount(&T::encrypt) == 4,
33
+ "Expected encrypt(...) to have 4 parameters");
34
+ return bridging::callFromJs<jsi::Value>(rt, &T::encrypt, static_cast<NativeAesGcmCxxSpec*>(&turboModule)->jsInvoker_, static_cast<T*>(&turboModule),
35
+ count <= 0 ? throw jsi::JSError(rt, "Expected argument in position 0 to be passed") : args[0].asString(rt),
36
+ count <= 1 ? throw jsi::JSError(rt, "Expected argument in position 1 to be passed") : args[1].asString(rt),
37
+ count <= 2 ? throw jsi::JSError(rt, "Expected argument in position 2 to be passed") : args[2].asNumber());
38
+ }
39
+
40
+ static jsi::Value __decrypt(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
41
+ static_assert(
42
+ bridging::getParameterCount(&T::decrypt) == 4,
43
+ "Expected decrypt(...) to have 4 parameters");
44
+ return bridging::callFromJs<jsi::Value>(rt, &T::decrypt, static_cast<NativeAesGcmCxxSpec*>(&turboModule)->jsInvoker_, static_cast<T*>(&turboModule),
45
+ count <= 0 ? throw jsi::JSError(rt, "Expected argument in position 0 to be passed") : args[0].asString(rt),
46
+ count <= 1 ? throw jsi::JSError(rt, "Expected argument in position 1 to be passed") : args[1].asString(rt),
47
+ count <= 2 ? throw jsi::JSError(rt, "Expected argument in position 2 to be passed") : args[2].asNumber());
48
+ }
49
+ };
50
+
51
+ } // namespace facebook::react
@@ -0,0 +1,5 @@
1
+ AesGcm_kotlinVersion=2.0.21
2
+ AesGcm_minSdkVersion=24
3
+ AesGcm_targetSdkVersion=34
4
+ AesGcm_compileSdkVersion=35
5
+ AesGcm_ndkVersion=27.1.12297006
@@ -0,0 +1,3 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="com.aesgcm">
3
+ </manifest>
@@ -0,0 +1,2 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>
@@ -0,0 +1,116 @@
1
+ package com.aesgcm
2
+
3
+ import android.util.Base64
4
+ import com.facebook.react.bridge.Promise
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.module.annotations.ReactModule
7
+ import java.nio.ByteBuffer
8
+ import java.nio.charset.StandardCharsets
9
+ import java.security.InvalidAlgorithmParameterException
10
+ import java.security.InvalidKeyException
11
+ import java.security.NoSuchAlgorithmException
12
+ import java.security.SecureRandom
13
+ import java.security.spec.InvalidKeySpecException
14
+ import java.security.spec.KeySpec
15
+ import javax.crypto.Cipher
16
+ import javax.crypto.NoSuchPaddingException
17
+ import javax.crypto.SecretKey
18
+ import javax.crypto.SecretKeyFactory
19
+ import javax.crypto.spec.GCMParameterSpec
20
+ import javax.crypto.spec.PBEKeySpec
21
+ import javax.crypto.spec.SecretKeySpec
22
+
23
+
24
+ @ReactModule(name = AesGcmModule.NAME)
25
+ class AesGcmModule(reactContext: ReactApplicationContext) : NativeAesGcmSpec(reactContext) {
26
+
27
+ override fun getName(): String {
28
+ return NAME
29
+ }
30
+
31
+ override fun encrypt(
32
+ plainText: String,
33
+ key: String,
34
+ iterationCount: Double,
35
+ promise: Promise
36
+ ) {
37
+ try {
38
+ val salt = getRandomNonce(SALT_LENGTH)
39
+ val secretKey: SecretKey = getAESKeyFromPassword(key.toCharArray(), salt, iterationCount)
40
+ val iv = getRandomNonce(IV_LENGTH)
41
+ val cipher = initCipher(Cipher.ENCRYPT_MODE, secretKey, iv)
42
+ val encryptedMessageByte = cipher.doFinal(plainText.toByteArray())
43
+ val cipherByte = ByteBuffer.allocate(salt.size + iv.size + encryptedMessageByte.size)
44
+ .put(salt)
45
+ .put(iv)
46
+ .put(encryptedMessageByte)
47
+ .array()
48
+ val result = Base64.encodeToString(cipherByte, Base64.NO_WRAP)
49
+ promise.resolve(result);
50
+ } catch (e: Exception) {
51
+ return promise.reject("ENCRYPT_ERROR", e.message)
52
+ }
53
+ }
54
+
55
+ override fun decrypt(
56
+ encryptedText: String,
57
+ key: String,
58
+ iterationCount: Double,
59
+ promise: Promise
60
+ ) {
61
+ try {
62
+ val decode: ByteArray = Base64.decode(
63
+ encryptedText.toByteArray(StandardCharsets.UTF_8),
64
+ Base64.NO_WRAP
65
+ )
66
+ val byteBuffer = ByteBuffer.wrap(decode)
67
+ val salt = ByteArray(SALT_LENGTH)
68
+ byteBuffer.get(salt)
69
+ val iv = ByteArray(IV_LENGTH)
70
+ byteBuffer.get(iv)
71
+ val content = ByteArray(byteBuffer.remaining())
72
+ byteBuffer.get(content)
73
+ val cipher = Cipher.getInstance(CIPHER_ALGORITHM)
74
+ val aesKeyFromPassword: SecretKeySpec = getAESKeyFromPassword(key.toCharArray(), salt, iterationCount)
75
+ cipher.init(Cipher.DECRYPT_MODE, aesKeyFromPassword, GCMParameterSpec(GCM_TAG_LENGTH * 8, iv))
76
+ val plainText = String(cipher.doFinal(content))
77
+ promise.resolve(plainText);
78
+ } catch (e: java.lang.Exception) {
79
+ promise.reject("DECRYPT_ERROR", e.message)
80
+ }
81
+ }
82
+
83
+ private fun getRandomNonce(length: Int): ByteArray {
84
+ val nonce = ByteArray(length)
85
+ SecureRandom().nextBytes(nonce)
86
+ return nonce
87
+ }
88
+
89
+ @Throws(
90
+ InvalidKeyException::class,
91
+ InvalidAlgorithmParameterException::class,
92
+ NoSuchPaddingException::class,
93
+ NoSuchAlgorithmException::class
94
+ )
95
+ private fun initCipher(mode: Int, secretKey: SecretKey?, iv: ByteArray): Cipher {
96
+ val cipher = Cipher.getInstance(CIPHER_ALGORITHM)
97
+ cipher.init(mode, secretKey, GCMParameterSpec(GCM_TAG_LENGTH * 8, iv))
98
+ return cipher
99
+ }
100
+
101
+ @Throws(NoSuchAlgorithmException::class, InvalidKeySpecException::class)
102
+ private fun getAESKeyFromPassword(password: CharArray?, salt: ByteArray, iterationCount: Double): SecretKeySpec {
103
+ val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512")
104
+ val spec: KeySpec = PBEKeySpec(password, salt, iterationCount.toInt(), KEY_LENGTH * 8)
105
+ return SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES")
106
+ }
107
+
108
+ companion object {
109
+ const val NAME = "AesGcm"
110
+ private const val GCM_TAG_LENGTH: Int = 16
111
+ private const val SALT_LENGTH: Int = 16
112
+ private const val IV_LENGTH: Int = 12
113
+ private const val KEY_LENGTH: Int = 32
114
+ private const val CIPHER_ALGORITHM: String = "AES/GCM/NoPadding"
115
+ }
116
+ }
@@ -0,0 +1,33 @@
1
+ package com.aesgcm
2
+
3
+ import com.facebook.react.BaseReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.module.model.ReactModuleInfo
7
+ import com.facebook.react.module.model.ReactModuleInfoProvider
8
+ import java.util.HashMap
9
+
10
+ class AesGcmPackage : BaseReactPackage() {
11
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12
+ return if (name == AesGcmModule.NAME) {
13
+ AesGcmModule(reactContext)
14
+ } else {
15
+ null
16
+ }
17
+ }
18
+
19
+ override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
20
+ return ReactModuleInfoProvider {
21
+ val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
22
+ moduleInfos[AesGcmModule.NAME] = ReactModuleInfo(
23
+ AesGcmModule.NAME,
24
+ AesGcmModule.NAME,
25
+ false, // canOverrideExistingModule
26
+ false, // needsEagerInit
27
+ false, // isCxxModule
28
+ true // isTurboModule
29
+ )
30
+ moduleInfos
31
+ }
32
+ }
33
+ }
package/ios/AesGcm.h ADDED
@@ -0,0 +1,6 @@
1
+
2
+ #import <AesGcm/RNAesGcmSpec.h>
3
+
4
+ @interface AesGcm : NSObject <NativeAesGcmSpec>
5
+
6
+ @end
package/ios/AesGcm.mm ADDED
@@ -0,0 +1,66 @@
1
+ #import "AesGcm.h"
2
+ #import "AesGcm-Swift.h"
3
+
4
+ @implementation AesGcm
5
+ RCT_EXPORT_MODULE()
6
+
7
+ #pragma mark - Init
8
+ - (instancetype)init {
9
+ if (self = [super init]) {
10
+ _manager = [EncryptionManager new];
11
+ }
12
+ return self;
13
+ }
14
+
15
+ #pragma mark - Properties
16
+ EncryptionManager *_manager;
17
+
18
+ - (void)encrypt:(NSString *)plainText
19
+ key:(NSString *)key
20
+ iterationCount:(double)iterationCount
21
+ resolve:(RCTPromiseResolveBlock)resolve
22
+ reject:(RCTPromiseRejectBlock)reject {
23
+
24
+ NSError *error = nil;
25
+ NSString *result = [_manager encrypt:plainText
26
+ key:key
27
+ iterationCount:@((int)iterationCount)
28
+ error:&error];
29
+ if (result != nil) {
30
+ resolve(result);
31
+ } else {
32
+ reject(@"encrypt_error",
33
+ error.localizedDescription ?: @"Encryption failed",
34
+ error);
35
+ }
36
+
37
+ }
38
+
39
+ - (void)decrypt:(NSString *)encryptedText
40
+ key:(NSString *)key
41
+ iterationCount:(double)iterationCount
42
+ resolve:(RCTPromiseResolveBlock)resolve
43
+ reject:(RCTPromiseRejectBlock)reject {
44
+ NSError *error = nil;
45
+
46
+ NSString *result = [_manager decrypt:encryptedText
47
+ key:key
48
+ iterationCount:@((int)iterationCount)
49
+ error:&error];
50
+
51
+ if (result != nil) {
52
+ resolve(result);
53
+ } else {
54
+ reject(@"decrypt_error",
55
+ error.localizedDescription ?: @"Decryption failed",
56
+ error);
57
+ }
58
+ }
59
+
60
+ - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
61
+ (const facebook::react::ObjCTurboModule::InitParams &)params
62
+ {
63
+ return std::make_shared<facebook::react::NativeAesGcmSpecJSI>(params);
64
+ }
65
+
66
+ @end