@onekeyfe/react-native-aes-crypto 1.1.55 → 1.1.57

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.
@@ -0,0 +1,77 @@
1
+ buildscript {
2
+ ext.getExtOrDefault = {name ->
3
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['AesCrypto_' + 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["AesCrypto_" + name]).toInteger()
26
+ }
27
+
28
+ android {
29
+ namespace "com.aescrypto"
30
+
31
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
32
+
33
+ defaultConfig {
34
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
35
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
36
+ }
37
+
38
+ buildFeatures {
39
+ buildConfig true
40
+ }
41
+
42
+ buildTypes {
43
+ release {
44
+ minifyEnabled false
45
+ }
46
+ }
47
+
48
+ lintOptions {
49
+ disable "GradleCompatible"
50
+ }
51
+
52
+ compileOptions {
53
+ sourceCompatibility JavaVersion.VERSION_1_8
54
+ targetCompatibility JavaVersion.VERSION_1_8
55
+ }
56
+
57
+ sourceSets {
58
+ main {
59
+ java.srcDirs += [
60
+ "generated/java",
61
+ "generated/jni"
62
+ ]
63
+ }
64
+ }
65
+ }
66
+
67
+ repositories {
68
+ mavenCentral()
69
+ google()
70
+ }
71
+
72
+ def kotlin_version = getExtOrDefault("kotlinVersion")
73
+
74
+ dependencies {
75
+ implementation "com.facebook.react:react-android"
76
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
77
+ }
@@ -0,0 +1,202 @@
1
+ package com.aescrypto
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.security.MessageDigest
8
+ import java.security.SecureRandom
9
+ import java.util.UUID
10
+ import javax.crypto.Cipher
11
+ import javax.crypto.Mac
12
+ import javax.crypto.SecretKeyFactory
13
+ import javax.crypto.spec.IvParameterSpec
14
+ import javax.crypto.spec.PBEKeySpec
15
+ import javax.crypto.spec.SecretKeySpec
16
+
17
+ @ReactModule(name = AesCryptoModule.NAME)
18
+ class AesCryptoModule(reactContext: ReactApplicationContext) :
19
+ NativeAesCryptoSpec(reactContext) {
20
+
21
+ companion object {
22
+ const val NAME = "AesCrypto"
23
+ }
24
+
25
+ override fun getName(): String = NAME
26
+
27
+ private fun cipherTransformation(algorithm: String): String {
28
+ return when (algorithm.lowercase()) {
29
+ "aes-128-cbc", "aes-192-cbc", "aes-256-cbc" -> "AES/CBC/PKCS5Padding"
30
+ "aes-128-ecb", "aes-192-ecb", "aes-256-ecb" -> "AES/ECB/PKCS5Padding"
31
+ else -> "AES/CBC/PKCS5Padding"
32
+ }
33
+ }
34
+
35
+ override fun encrypt(data: String, key: String, iv: String, algorithm: String, promise: Promise) {
36
+ Thread {
37
+ try {
38
+ val keyBytes = hexToBytes(key)
39
+ val ivBytes = hexToBytes(iv)
40
+ val transformation = cipherTransformation(algorithm)
41
+ val cipher = Cipher.getInstance(transformation)
42
+ val secretKey = SecretKeySpec(keyBytes, "AES")
43
+ if (transformation.contains("ECB")) {
44
+ cipher.init(Cipher.ENCRYPT_MODE, secretKey)
45
+ } else {
46
+ cipher.init(Cipher.ENCRYPT_MODE, secretKey, IvParameterSpec(ivBytes))
47
+ }
48
+ val encrypted = cipher.doFinal(data.toByteArray(Charsets.UTF_8))
49
+ promise.resolve(Base64.encodeToString(encrypted, Base64.NO_WRAP))
50
+ } catch (e: Exception) {
51
+ promise.reject("AES_CRYPTO_ERROR", e.message, e)
52
+ }
53
+ }.start()
54
+ }
55
+
56
+ override fun decrypt(base64: String, key: String, iv: String, algorithm: String, promise: Promise) {
57
+ Thread {
58
+ try {
59
+ val keyBytes = hexToBytes(key)
60
+ val ivBytes = hexToBytes(iv)
61
+ val transformation = cipherTransformation(algorithm)
62
+ val cipher = Cipher.getInstance(transformation)
63
+ val secretKey = SecretKeySpec(keyBytes, "AES")
64
+ if (transformation.contains("ECB")) {
65
+ cipher.init(Cipher.DECRYPT_MODE, secretKey)
66
+ } else {
67
+ cipher.init(Cipher.DECRYPT_MODE, secretKey, IvParameterSpec(ivBytes))
68
+ }
69
+ val decrypted = cipher.doFinal(Base64.decode(base64, Base64.NO_WRAP))
70
+ promise.resolve(String(decrypted, Charsets.UTF_8))
71
+ } catch (e: Exception) {
72
+ promise.reject("AES_CRYPTO_ERROR", e.message, e)
73
+ }
74
+ }.start()
75
+ }
76
+
77
+ override fun pbkdf2(password: String, salt: String, cost: Double, length: Double, algorithm: String, promise: Promise) {
78
+ Thread {
79
+ try {
80
+ val saltBytes = salt.toByteArray(Charsets.UTF_8)
81
+ val iterationCount = cost.toInt()
82
+ val keyLength = length.toInt() * 8
83
+ val hmacAlgorithm = when (algorithm.uppercase()) {
84
+ "SHA256", "SHA-256" -> "PBKDF2WithHmacSHA256"
85
+ "SHA512", "SHA-512" -> "PBKDF2WithHmacSHA512"
86
+ else -> "PBKDF2WithHmacSHA1"
87
+ }
88
+ val spec = PBEKeySpec(password.toCharArray(), saltBytes, iterationCount, keyLength)
89
+ val factory = SecretKeyFactory.getInstance(hmacAlgorithm)
90
+ val keyBytes = factory.generateSecret(spec).encoded
91
+ promise.resolve(bytesToHex(keyBytes))
92
+ } catch (e: Exception) {
93
+ promise.reject("AES_CRYPTO_ERROR", e.message, e)
94
+ }
95
+ }.start()
96
+ }
97
+
98
+ override fun hmac256(base64: String, key: String, promise: Promise) {
99
+ Thread {
100
+ try {
101
+ val mac = Mac.getInstance("HmacSHA256")
102
+ val secretKey = SecretKeySpec(hexToBytes(key), "HmacSHA256")
103
+ mac.init(secretKey)
104
+ val result = mac.doFinal(Base64.decode(base64, Base64.NO_WRAP))
105
+ promise.resolve(bytesToHex(result))
106
+ } catch (e: Exception) {
107
+ promise.reject("AES_CRYPTO_ERROR", e.message, e)
108
+ }
109
+ }.start()
110
+ }
111
+
112
+ override fun hmac512(base64: String, key: String, promise: Promise) {
113
+ Thread {
114
+ try {
115
+ val mac = Mac.getInstance("HmacSHA512")
116
+ val secretKey = SecretKeySpec(hexToBytes(key), "HmacSHA512")
117
+ mac.init(secretKey)
118
+ val result = mac.doFinal(Base64.decode(base64, Base64.NO_WRAP))
119
+ promise.resolve(bytesToHex(result))
120
+ } catch (e: Exception) {
121
+ promise.reject("AES_CRYPTO_ERROR", e.message, e)
122
+ }
123
+ }.start()
124
+ }
125
+
126
+ override fun sha1(text: String, promise: Promise) {
127
+ Thread {
128
+ try {
129
+ val digest = MessageDigest.getInstance("SHA-1")
130
+ val result = digest.digest(text.toByteArray(Charsets.UTF_8))
131
+ promise.resolve(bytesToHex(result))
132
+ } catch (e: Exception) {
133
+ promise.reject("AES_CRYPTO_ERROR", e.message, e)
134
+ }
135
+ }.start()
136
+ }
137
+
138
+ override fun sha256(text: String, promise: Promise) {
139
+ Thread {
140
+ try {
141
+ val digest = MessageDigest.getInstance("SHA-256")
142
+ val result = digest.digest(text.toByteArray(Charsets.UTF_8))
143
+ promise.resolve(bytesToHex(result))
144
+ } catch (e: Exception) {
145
+ promise.reject("AES_CRYPTO_ERROR", e.message, e)
146
+ }
147
+ }.start()
148
+ }
149
+
150
+ override fun sha512(text: String, promise: Promise) {
151
+ Thread {
152
+ try {
153
+ val digest = MessageDigest.getInstance("SHA-512")
154
+ val result = digest.digest(text.toByteArray(Charsets.UTF_8))
155
+ promise.resolve(bytesToHex(result))
156
+ } catch (e: Exception) {
157
+ promise.reject("AES_CRYPTO_ERROR", e.message, e)
158
+ }
159
+ }.start()
160
+ }
161
+
162
+ override fun randomUuid(promise: Promise) {
163
+ Thread {
164
+ try {
165
+ promise.resolve(UUID.randomUUID().toString())
166
+ } catch (e: Exception) {
167
+ promise.reject("AES_CRYPTO_ERROR", e.message, e)
168
+ }
169
+ }.start()
170
+ }
171
+
172
+ override fun randomKey(length: Double, promise: Promise) {
173
+ Thread {
174
+ try {
175
+ val bytes = ByteArray(length.toInt())
176
+ SecureRandom().nextBytes(bytes)
177
+ promise.resolve(bytesToHex(bytes))
178
+ } catch (e: Exception) {
179
+ promise.reject("AES_CRYPTO_ERROR", e.message, e)
180
+ }
181
+ }.start()
182
+ }
183
+
184
+ private fun hexToBytes(hex: String): ByteArray {
185
+ val len = hex.length
186
+ val data = ByteArray(len / 2)
187
+ var i = 0
188
+ while (i < len) {
189
+ data[i / 2] = ((Character.digit(hex[i], 16) shl 4) + Character.digit(hex[i + 1], 16)).toByte()
190
+ i += 2
191
+ }
192
+ return data
193
+ }
194
+
195
+ private fun bytesToHex(bytes: ByteArray): String {
196
+ val sb = StringBuilder()
197
+ for (b in bytes) {
198
+ sb.append(String.format("%02x", b))
199
+ }
200
+ return sb.toString()
201
+ }
202
+ }
@@ -0,0 +1,33 @@
1
+ package com.aescrypto
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 AesCryptoPackage : BaseReactPackage() {
11
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12
+ return if (name == AesCryptoModule.NAME) {
13
+ AesCryptoModule(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[AesCryptoModule.NAME] = ReactModuleInfo(
23
+ AesCryptoModule.NAME,
24
+ AesCryptoModule.NAME,
25
+ false, // canOverrideExistingModule
26
+ false, // needsEagerInit
27
+ false, // isCxxModule
28
+ true // isTurboModule
29
+ )
30
+ moduleInfos
31
+ }
32
+ }
33
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-aes-crypto",
3
- "version": "1.1.55",
3
+ "version": "1.1.57",
4
4
  "description": "react-native-aes-crypto",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -21,7 +21,8 @@
21
21
  "!**/__tests__",
22
22
  "!**/__fixtures__",
23
23
  "!**/__mocks__",
24
- "!**/.*"
24
+ "!**/.*",
25
+ "android"
25
26
  ],
26
27
  "scripts": {
27
28
  "clean": "del-cli ios/build lib",