@functionland/react-native-fula 1.14.8 → 1.19.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 (141) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +226 -214
  3. package/android/build.gradle +110 -110
  4. package/android/gradle.properties +5 -5
  5. package/android/src/main/AndroidManifest.xml +3 -3
  6. package/android/src/main/AndroidManifestNew.xml +2 -2
  7. package/android/src/main/java/land/fx/fula/ConfigRef.java +7 -7
  8. package/android/src/main/java/land/fx/fula/Cryptography.java +62 -62
  9. package/android/src/main/java/land/fx/fula/FulaModule.java +1435 -1466
  10. package/android/src/main/java/land/fx/fula/FulaPackage.java +32 -32
  11. package/android/src/main/java/land/fx/fula/SharedPreferenceHelper.java +65 -65
  12. package/android/src/main/java/land/fx/fula/StaticHelper.java +13 -13
  13. package/android/src/main/java/land/fx/fula/ThreadUtils.java +42 -42
  14. package/ios/Cryptography.swift +49 -49
  15. package/ios/Fula-Bridging-Header.h +3 -3
  16. package/ios/Fula.mm +197 -199
  17. package/ios/Fula.swift +1221 -1221
  18. package/ios/UserDataHelper.swift +143 -143
  19. package/lib/commonjs/index.js +2 -2
  20. package/lib/commonjs/index.js.map +1 -1
  21. package/lib/commonjs/interfaces/augment-api-errors.js +4 -0
  22. package/lib/commonjs/interfaces/augment-api-errors.js.map +1 -0
  23. package/lib/commonjs/interfaces/augment-api-rpc.js +4 -0
  24. package/lib/commonjs/interfaces/augment-api-rpc.js.map +1 -0
  25. package/lib/commonjs/interfaces/augment-api-runtime.js +4 -0
  26. package/lib/commonjs/interfaces/augment-api-runtime.js.map +1 -0
  27. package/lib/commonjs/interfaces/augment-api.js +10 -0
  28. package/lib/commonjs/interfaces/augment-api.js.map +1 -0
  29. package/lib/commonjs/interfaces/augment-types.js +4 -0
  30. package/lib/commonjs/interfaces/augment-types.js.map +1 -0
  31. package/lib/commonjs/interfaces/fulaNativeModule.js +1 -2
  32. package/lib/commonjs/interfaces/fulaNativeModule.js.map +1 -1
  33. package/lib/commonjs/interfaces/index.js +6 -0
  34. package/lib/commonjs/interfaces/index.js.map +1 -0
  35. package/lib/commonjs/interfaces/{api-lookup.js → lookup.js} +623 -382
  36. package/lib/commonjs/interfaces/lookup.js.map +1 -0
  37. package/lib/commonjs/interfaces/registry.js +4 -0
  38. package/lib/commonjs/interfaces/registry.js.map +1 -0
  39. package/lib/commonjs/interfaces/types-lookup.js +4 -0
  40. package/lib/commonjs/interfaces/types-lookup.js.map +1 -0
  41. package/lib/commonjs/interfaces/types.js +4 -0
  42. package/lib/commonjs/interfaces/types.js.map +1 -0
  43. package/lib/commonjs/protocols/blockchain.js +123 -76
  44. package/lib/commonjs/protocols/blockchain.js.map +1 -1
  45. package/lib/commonjs/protocols/chain-api.js +151 -37
  46. package/lib/commonjs/protocols/chain-api.js.map +1 -1
  47. package/lib/commonjs/protocols/fula.js +195 -99
  48. package/lib/commonjs/protocols/fula.js.map +1 -1
  49. package/lib/commonjs/protocols/fxblox.js +3 -3
  50. package/lib/commonjs/protocols/fxblox.js.map +1 -1
  51. package/lib/commonjs/types/blockchain.js.map +1 -1
  52. package/lib/commonjs/types/fxblox.js.map +1 -1
  53. package/lib/module/index.js.map +1 -1
  54. package/lib/module/interfaces/augment-api-errors.js +9 -0
  55. package/lib/module/interfaces/augment-api-errors.js.map +1 -0
  56. package/lib/module/interfaces/augment-api-rpc.js +9 -0
  57. package/lib/module/interfaces/augment-api-rpc.js.map +1 -0
  58. package/lib/module/interfaces/augment-api-runtime.js +9 -0
  59. package/lib/module/interfaces/augment-api-runtime.js.map +1 -0
  60. package/lib/module/interfaces/augment-api.js +11 -0
  61. package/lib/module/interfaces/augment-api.js.map +1 -0
  62. package/lib/module/interfaces/augment-types.js +9 -0
  63. package/lib/module/interfaces/augment-types.js.map +1 -0
  64. package/lib/module/interfaces/fulaNativeModule.js.map +1 -1
  65. package/lib/module/interfaces/index.js +5 -0
  66. package/lib/module/interfaces/index.js.map +1 -0
  67. package/lib/module/interfaces/{api-lookup.js → lookup.js} +622 -380
  68. package/lib/module/interfaces/lookup.js.map +1 -0
  69. package/lib/module/interfaces/registry.js +9 -0
  70. package/lib/module/interfaces/registry.js.map +1 -0
  71. package/lib/module/interfaces/types-lookup.js +9 -0
  72. package/lib/module/interfaces/types-lookup.js.map +1 -0
  73. package/lib/module/interfaces/types.js +3 -0
  74. package/lib/module/interfaces/types.js.map +1 -0
  75. package/lib/module/protocols/blockchain.js +120 -75
  76. package/lib/module/protocols/blockchain.js.map +1 -1
  77. package/lib/module/protocols/chain-api.js +142 -31
  78. package/lib/module/protocols/chain-api.js.map +1 -1
  79. package/lib/module/protocols/fula.js +191 -99
  80. package/lib/module/protocols/fula.js.map +1 -1
  81. package/lib/module/protocols/fxblox.js +3 -3
  82. package/lib/module/protocols/fxblox.js.map +1 -1
  83. package/lib/module/types/blockchain.js.map +1 -1
  84. package/lib/module/types/fxblox.js.map +1 -1
  85. package/lib/typescript/interfaces/augment-api-errors.d.ts +442 -0
  86. package/lib/typescript/interfaces/augment-api-errors.d.ts.map +1 -0
  87. package/lib/typescript/interfaces/augment-api-rpc.d.ts +680 -0
  88. package/lib/typescript/interfaces/augment-api-rpc.d.ts.map +1 -0
  89. package/lib/typescript/interfaces/augment-api-runtime.d.ts +248 -0
  90. package/lib/typescript/interfaces/augment-api-runtime.d.ts.map +1 -0
  91. package/lib/typescript/interfaces/augment-api.d.ts +8 -0
  92. package/lib/typescript/interfaces/augment-api.d.ts.map +1 -0
  93. package/lib/typescript/interfaces/augment-types.d.ts +1224 -0
  94. package/lib/typescript/interfaces/augment-types.d.ts.map +1 -0
  95. package/lib/typescript/interfaces/fulaNativeModule.d.ts +7 -3
  96. package/lib/typescript/interfaces/fulaNativeModule.d.ts.map +1 -1
  97. package/lib/typescript/interfaces/index.d.ts +1 -0
  98. package/lib/typescript/interfaces/index.d.ts.map +1 -0
  99. package/lib/typescript/interfaces/{api-lookup.d.ts → lookup.d.ts} +441 -199
  100. package/lib/typescript/interfaces/lookup.d.ts.map +1 -0
  101. package/lib/typescript/interfaces/registry.d.ts +146 -0
  102. package/lib/typescript/interfaces/registry.d.ts.map +1 -0
  103. package/lib/typescript/interfaces/types-lookup.d.ts +1910 -0
  104. package/lib/typescript/interfaces/types-lookup.d.ts.map +1 -0
  105. package/lib/typescript/interfaces/types.d.ts +1 -0
  106. package/lib/typescript/interfaces/types.d.ts.map +1 -0
  107. package/lib/typescript/protocols/blockchain.d.ts +5 -3
  108. package/lib/typescript/protocols/blockchain.d.ts.map +1 -1
  109. package/lib/typescript/protocols/chain-api.d.ts +5 -0
  110. package/lib/typescript/protocols/chain-api.d.ts.map +1 -1
  111. package/lib/typescript/protocols/fula.d.ts +16 -0
  112. package/lib/typescript/protocols/fula.d.ts.map +1 -1
  113. package/lib/typescript/types/blockchain.d.ts +6 -0
  114. package/lib/typescript/types/blockchain.d.ts.map +1 -1
  115. package/package.json +178 -172
  116. package/react-native-fula.podspec +47 -47
  117. package/src/index.tsx +4 -4
  118. package/src/interfaces/augment-api-errors.ts +449 -0
  119. package/src/interfaces/augment-api-rpc.ts +617 -0
  120. package/src/interfaces/augment-api-runtime.ts +224 -0
  121. package/src/interfaces/augment-api.ts +10 -0
  122. package/src/interfaces/augment-types.ts +1230 -0
  123. package/src/interfaces/fulaNativeModule.ts +130 -122
  124. package/src/interfaces/index.ts +4 -0
  125. package/src/interfaces/{api-lookup.ts → lookup.ts} +1889 -1647
  126. package/src/interfaces/registry.ts +152 -0
  127. package/src/interfaces/types-lookup.ts +2043 -0
  128. package/src/interfaces/types.ts +3 -0
  129. package/src/protocols/blockchain.ts +549 -504
  130. package/src/protocols/chain-api.ts +148 -24
  131. package/src/protocols/fula.ts +419 -314
  132. package/src/protocols/fxblox.ts +49 -49
  133. package/src/types/blockchain.ts +89 -81
  134. package/src/types/fxblox.ts +8 -8
  135. package/ios/Fula.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  136. package/ios/Fula.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  137. package/ios/Fula.xcodeproj/project.xcworkspace/xcuserdata/user246549.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  138. package/ios/Fula.xcodeproj/xcuserdata/user246549.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
  139. package/lib/commonjs/interfaces/api-lookup.js.map +0 -1
  140. package/lib/module/interfaces/api-lookup.js.map +0 -1
  141. package/lib/typescript/interfaces/api-lookup.d.ts.map +0 -1
@@ -1,110 +1,110 @@
1
- buildscript {
2
- repositories {
3
- google()
4
- mavenCentral()
5
- }
6
-
7
- dependencies {
8
- classpath "com.android.tools.build:gradle:7.2.1"
9
- }
10
- }
11
-
12
- def isNewArchitectureEnabled() {
13
- return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
14
- }
15
-
16
- apply plugin: "com.android.library"
17
-
18
-
19
- def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
20
-
21
- if (isNewArchitectureEnabled()) {
22
- apply plugin: "com.facebook.react"
23
- }
24
-
25
- def getExtOrDefault(name) {
26
- return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Fula_" + name]
27
- }
28
-
29
- def getExtOrIntegerDefault(name) {
30
- return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Fula_" + name]).toInteger()
31
- }
32
-
33
- def supportsNamespace() {
34
- def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
35
- def major = parsed[0].toInteger()
36
- def minor = parsed[1].toInteger()
37
-
38
- // Namespace support was added in 7.3.0
39
- if (major == 7 && minor >= 3) {
40
- return true
41
- }
42
-
43
- return major >= 8
44
- }
45
-
46
- android {
47
- if (supportsNamespace()) {
48
- namespace "land.fx.fula"
49
-
50
- sourceSets {
51
- main {
52
- manifest.srcFile "src/main/AndroidManifestNew.xml"
53
- }
54
- }
55
- }
56
-
57
- compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
58
-
59
- defaultConfig {
60
- minSdkVersion getExtOrIntegerDefault("minSdkVersion")
61
- targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
62
- buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
63
- }
64
- buildTypes {
65
- release {
66
- minifyEnabled false
67
- }
68
- }
69
-
70
- lintOptions {
71
- disable "GradleCompatible"
72
- }
73
-
74
- compileOptions {
75
- sourceCompatibility JavaVersion.VERSION_1_8
76
- targetCompatibility JavaVersion.VERSION_1_8
77
- }
78
- ndkVersion '25.1.8937393'
79
- }
80
-
81
- repositories {
82
- mavenLocal()
83
- google()
84
- jcenter()
85
- mavenCentral()
86
-
87
- // Add this repo to get go-fula package
88
- maven { url 'https://jitpack.io' }
89
- }
90
-
91
-
92
- dependencies {
93
- // For < 0.71, this will be from the local maven repo
94
- // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
95
- //noinspection GradleDynamicVersion
96
- implementation "com.facebook.react:react-native:+"
97
- implementation 'com.github.functionland:fula-build-aar:v1.14.3' // From jitpack.io
98
- implementation 'com.github.functionland:wnfs-android:v1.8.1' // From jitpack.io
99
- implementation 'commons-io:commons-io:20030203.000550'
100
- implementation 'commons-codec:commons-codec:1.15'
101
- // implementation files('mobile.aar')
102
- }
103
-
104
- if (isNewArchitectureEnabled()) {
105
- react {
106
- jsRootDir = file("../src/")
107
- libraryName = "Fula"
108
- codegenJavaPackageName = "land.fx.fula"
109
- }
110
- }
1
+ buildscript {
2
+ repositories {
3
+ google()
4
+ mavenCentral()
5
+ }
6
+
7
+ dependencies {
8
+ classpath "com.android.tools.build:gradle:7.2.2"
9
+ }
10
+ }
11
+
12
+ def isNewArchitectureEnabled() {
13
+ return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
14
+ }
15
+
16
+ apply plugin: "com.android.library"
17
+
18
+
19
+ def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
20
+
21
+ if (isNewArchitectureEnabled()) {
22
+ apply plugin: "com.facebook.react"
23
+ }
24
+
25
+ def getExtOrDefault(name) {
26
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Fula_" + name]
27
+ }
28
+
29
+ def getExtOrIntegerDefault(name) {
30
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Fula_" + name]).toInteger()
31
+ }
32
+
33
+ def supportsNamespace() {
34
+ def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
35
+ def major = parsed[0].toInteger()
36
+ def minor = parsed[1].toInteger()
37
+
38
+ // Namespace support was added in 7.3.0
39
+ if (major == 7 && minor >= 3) {
40
+ return true
41
+ }
42
+
43
+ return major >= 8
44
+ }
45
+
46
+ android {
47
+ if (supportsNamespace()) {
48
+ namespace "land.fx.fula"
49
+
50
+ sourceSets {
51
+ main {
52
+ manifest.srcFile "src/main/AndroidManifestNew.xml"
53
+ }
54
+ }
55
+ }
56
+
57
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
58
+
59
+ defaultConfig {
60
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
61
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
62
+ buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
63
+ }
64
+ buildTypes {
65
+ release {
66
+ minifyEnabled false
67
+ }
68
+ }
69
+
70
+ lintOptions {
71
+ disable "GradleCompatible"
72
+ }
73
+
74
+ compileOptions {
75
+ sourceCompatibility JavaVersion.VERSION_1_8
76
+ targetCompatibility JavaVersion.VERSION_1_8
77
+ }
78
+ ndkVersion '25.1.8937393'
79
+ }
80
+
81
+ repositories {
82
+ mavenLocal()
83
+ google()
84
+ jcenter()
85
+ mavenCentral()
86
+
87
+ // Add this repo to get go-fula package
88
+ maven { url 'https://jitpack.io' }
89
+ }
90
+
91
+
92
+ dependencies {
93
+ // For < 0.71, this will be from the local maven repo
94
+ // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
95
+ //noinspection GradleDynamicVersion
96
+ implementation "com.facebook.react:react-native:+"
97
+ implementation 'com.github.functionland:fula-build-aar:v1.19.0' // From jitpack.io
98
+ implementation 'com.github.functionland:wnfs-android:v1.8.1' // From jitpack.io
99
+ implementation 'commons-io:commons-io:20030203.000550'
100
+ implementation 'commons-codec:commons-codec:1.15'
101
+ // implementation files('mobile.aar')
102
+ }
103
+
104
+ if (isNewArchitectureEnabled()) {
105
+ react {
106
+ jsRootDir = file("../src/")
107
+ libraryName = "Fula"
108
+ codegenJavaPackageName = "land.fx.fula"
109
+ }
110
+ }
@@ -1,5 +1,5 @@
1
- Fula_kotlinVersion=1.7.0
2
- Fula_minSdkVersion=21
3
- Fula_targetSdkVersion=31
4
- Fula_compileSdkVersion=31
5
- Fula_ndkversion=21.4.7075529
1
+ Fula_kotlinVersion=1.7.0
2
+ Fula_minSdkVersion=26
3
+ Fula_targetSdkVersion=31
4
+ Fula_compileSdkVersion=31
5
+ Fula_ndkversion=21.4.7075529
@@ -1,3 +1,3 @@
1
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
- package="land.fx.fula">
3
- </manifest>
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="land.fx.fula">
3
+ </manifest>
@@ -1,2 +1,2 @@
1
- <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
- </manifest>
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>
@@ -1,7 +1,7 @@
1
- package land.fx.fula;
2
-
3
- public final class ConfigRef {
4
- public byte[] identity;
5
- public java.lang.String storePath;
6
- }
7
-
1
+ package land.fx.fula;
2
+
3
+ public final class ConfigRef {
4
+ public byte[] identity;
5
+ public java.lang.String storePath;
6
+ }
7
+
@@ -1,62 +1,62 @@
1
- package land.fx.fula;
2
-
3
- import android.util.Base64;
4
-
5
- import java.io.UnsupportedEncodingException;
6
- import java.nio.charset.StandardCharsets;
7
- import java.security.InvalidAlgorithmParameterException;
8
- import java.security.InvalidKeyException;
9
- import java.security.NoSuchAlgorithmException;
10
- import java.security.spec.InvalidKeySpecException;
11
- import java.security.SecureRandom;
12
- import java.nio.ByteBuffer;
13
- import java.security.spec.InvalidParameterSpecException;
14
-
15
- import javax.crypto.BadPaddingException;
16
- import javax.crypto.Cipher;
17
- import javax.crypto.IllegalBlockSizeException;
18
- import javax.crypto.NoSuchPaddingException;
19
- import javax.crypto.SecretKey;
20
- import javax.crypto.SecretKeyFactory;
21
- import javax.crypto.spec.PBEKeySpec;
22
- import javax.crypto.spec.SecretKeySpec;
23
- import javax.crypto.spec.GCMParameterSpec;
24
-
25
- public class Cryptography {
26
- public static String encryptMsg(String message, SecretKey secret, byte[] iv)
27
- throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
28
- Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
29
- if (iv == null || iv.length == 0) {
30
- iv = new byte[12]; // Ensure this is randomly generated for each encryption.
31
- new SecureRandom().nextBytes(iv);
32
- }
33
- GCMParameterSpec spec = new GCMParameterSpec(128, iv);
34
- cipher.init(Cipher.ENCRYPT_MODE, secret, spec);
35
- byte[] cipherText = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
36
- ByteBuffer byteBuffer = ByteBuffer.allocate(iv.length + cipherText.length);
37
- byteBuffer.put(iv);
38
- byteBuffer.put(cipherText);
39
- return Base64.encodeToString(byteBuffer.array(), Base64.NO_WRAP);
40
- }
41
-
42
- public static String decryptMsg(String cipherText, SecretKey secret)
43
- throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
44
- ByteBuffer byteBuffer = ByteBuffer.wrap(Base64.decode(cipherText, Base64.NO_WRAP));
45
- byte[] iv = new byte[12];
46
- byteBuffer.get(iv);
47
- byte[] cipherBytes = new byte[byteBuffer.remaining()];
48
- byteBuffer.get(cipherBytes);
49
- Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
50
- GCMParameterSpec spec = new GCMParameterSpec(128, iv);
51
- cipher.init(Cipher.DECRYPT_MODE, secret, spec);
52
- String decryptString = new String(cipher.doFinal(cipherBytes), StandardCharsets.UTF_8);
53
- return decryptString;
54
- }
55
-
56
- public static SecretKey generateKey(byte[] key)
57
- throws NoSuchAlgorithmException, InvalidKeySpecException {
58
- PBEKeySpec pbeKeySpec = new PBEKeySpec(StaticHelper.bytesToBase64(key).toCharArray(), key, 1000, 128);
59
- SecretKey pbeKey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(pbeKeySpec);
60
- return new SecretKeySpec(pbeKey.getEncoded(), "AES");
61
- }
62
- }
1
+ package land.fx.fula;
2
+
3
+ import android.util.Base64;
4
+
5
+ import java.io.UnsupportedEncodingException;
6
+ import java.nio.charset.StandardCharsets;
7
+ import java.security.InvalidAlgorithmParameterException;
8
+ import java.security.InvalidKeyException;
9
+ import java.security.NoSuchAlgorithmException;
10
+ import java.security.spec.InvalidKeySpecException;
11
+ import java.security.SecureRandom;
12
+ import java.nio.ByteBuffer;
13
+ import java.security.spec.InvalidParameterSpecException;
14
+
15
+ import javax.crypto.BadPaddingException;
16
+ import javax.crypto.Cipher;
17
+ import javax.crypto.IllegalBlockSizeException;
18
+ import javax.crypto.NoSuchPaddingException;
19
+ import javax.crypto.SecretKey;
20
+ import javax.crypto.SecretKeyFactory;
21
+ import javax.crypto.spec.PBEKeySpec;
22
+ import javax.crypto.spec.SecretKeySpec;
23
+ import javax.crypto.spec.GCMParameterSpec;
24
+
25
+ public class Cryptography {
26
+ public static String encryptMsg(String message, SecretKey secret, byte[] iv)
27
+ throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
28
+ Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
29
+ if (iv == null || iv.length == 0) {
30
+ iv = new byte[12]; // Ensure this is randomly generated for each encryption.
31
+ new SecureRandom().nextBytes(iv);
32
+ }
33
+ GCMParameterSpec spec = new GCMParameterSpec(128, iv);
34
+ cipher.init(Cipher.ENCRYPT_MODE, secret, spec);
35
+ byte[] cipherText = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
36
+ ByteBuffer byteBuffer = ByteBuffer.allocate(iv.length + cipherText.length);
37
+ byteBuffer.put(iv);
38
+ byteBuffer.put(cipherText);
39
+ return Base64.encodeToString(byteBuffer.array(), Base64.NO_WRAP);
40
+ }
41
+
42
+ public static String decryptMsg(String cipherText, SecretKey secret)
43
+ throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
44
+ ByteBuffer byteBuffer = ByteBuffer.wrap(Base64.decode(cipherText, Base64.NO_WRAP));
45
+ byte[] iv = new byte[12];
46
+ byteBuffer.get(iv);
47
+ byte[] cipherBytes = new byte[byteBuffer.remaining()];
48
+ byteBuffer.get(cipherBytes);
49
+ Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
50
+ GCMParameterSpec spec = new GCMParameterSpec(128, iv);
51
+ cipher.init(Cipher.DECRYPT_MODE, secret, spec);
52
+ String decryptString = new String(cipher.doFinal(cipherBytes), StandardCharsets.UTF_8);
53
+ return decryptString;
54
+ }
55
+
56
+ public static SecretKey generateKey(byte[] key)
57
+ throws NoSuchAlgorithmException, InvalidKeySpecException {
58
+ PBEKeySpec pbeKeySpec = new PBEKeySpec(StaticHelper.bytesToBase64(key).toCharArray(), key, 1000, 128);
59
+ SecretKey pbeKey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(pbeKeySpec);
60
+ return new SecretKeySpec(pbeKey.getEncoded(), "AES");
61
+ }
62
+ }