Differential D7004 Diff 23853 native/expo-modules/aes-crypto/android/src/main/java/app/comm/android/aescrypto/AESCryptoModule.kt
Changeset View
Changeset View
Standalone View
Standalone View
native/expo-modules/aes-crypto/android/src/main/java/app/comm/android/aescrypto/AESCryptoModule.kt
package app.comm.android.aescrypto | package app.comm.android.aescrypto | ||||
import expo.modules.core.errors.CodedException | |||||
import expo.modules.kotlin.modules.Module | import expo.modules.kotlin.modules.Module | ||||
import expo.modules.kotlin.modules.ModuleDefinition | import expo.modules.kotlin.modules.ModuleDefinition | ||||
import expo.modules.kotlin.typedarray.Uint8Array | |||||
import java.security.SecureRandom | |||||
import javax.crypto.KeyGenerator | |||||
private const val ALGORITHM_AES = "AES" | |||||
private const val KEY_SIZE = 32 // bytes | |||||
class AESCryptoModule : Module() { | class AESCryptoModule : Module() { | ||||
private val secureRandom by lazy { SecureRandom() } | |||||
override fun definition() = ModuleDefinition { | override fun definition() = ModuleDefinition { | ||||
Name("AESCrypto") | Name("AESCrypto") | ||||
Function("hello") { | Function("generateKey", this@AESCryptoModule::generateKey) | ||||
return@Function "Hello world! 👋" | } | ||||
// region Function implementations | |||||
/** | |||||
* Generates AES-256 key and stores it in [destination] array | |||||
* | |||||
* @param destination JS typed array, must be [KEY_SIZE] bytes long | |||||
*/ | |||||
private fun generateKey(destination: Uint8Array) { | |||||
if (destination.byteLength != KEY_SIZE) { | |||||
throw InvalidKeyLengthException() | |||||
} | } | ||||
val keygen = KeyGenerator.getInstance(ALGORITHM_AES).apply { | |||||
init(KEY_SIZE * 8, secureRandom) | |||||
} | } | ||||
val keyBytes = keygen.generateKey().encoded | |||||
destination.write(keyBytes, position = 0, size = keyBytes.size) | |||||
} | } | ||||
// endregion | |||||
} | |||||
// region Exception definitions | |||||
private class InvalidKeyLengthException : | |||||
CodedException("The AES key has invalid length") | |||||
// endregion |