-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
284fe2e
commit db78138
Showing
5 changed files
with
81 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
iterableapi/src/main/java/com/iterable/iterableapi/IterableDataEncryptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.iterable.iterableapi | ||
|
||
import android.util.Base64 | ||
import javax.crypto.Cipher | ||
import javax.crypto.KeyGenerator | ||
import javax.crypto.SecretKey | ||
import javax.crypto.spec.GCMParameterSpec | ||
|
||
import com.iterable.iterableapi.IterableLogger | ||
|
||
class IterableDataEncryptor { | ||
private val TAG = "IterableDataEncryptor" | ||
private val key: SecretKey | ||
private val ALGORITHM = "AES/GCM/NoPadding" | ||
private val GCM_IV_LENGTH = 12 | ||
private val GCM_TAG_LENGTH = 128 | ||
|
||
init { | ||
// Generate a new key for this session | ||
val keyGen = KeyGenerator.getInstance("AES") | ||
keyGen.init(256) | ||
key = keyGen.generateKey() | ||
} | ||
|
||
fun encrypt(plaintext: String): String { | ||
try { | ||
val cipher = Cipher.getInstance(ALGORITHM) | ||
val iv = ByteArray(GCM_IV_LENGTH) | ||
// In a real implementation, you'd want to use SecureRandom here | ||
iv.fill(0) | ||
|
||
val parameterSpec = GCMParameterSpec(GCM_TAG_LENGTH, iv) | ||
cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec) | ||
|
||
val ciphertext = cipher.doFinal(plaintext.toByteArray()) | ||
return Base64.encodeToString(ciphertext, Base64.NO_WRAP) | ||
} catch (e: Exception) { | ||
IterableLogger.e(TAG, "Encryption failed", e) | ||
return plaintext // Fallback to plaintext if encryption fails | ||
} | ||
} | ||
|
||
fun decrypt(encryptedData: String): String { | ||
try { | ||
val cipher = Cipher.getInstance(ALGORITHM) | ||
val iv = ByteArray(GCM_IV_LENGTH) | ||
// Use same IV as encryption | ||
iv.fill(0) | ||
|
||
val parameterSpec = GCMParameterSpec(GCM_TAG_LENGTH, iv) | ||
cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec) | ||
|
||
val ciphertext = Base64.decode(encryptedData, Base64.NO_WRAP) | ||
return String(cipher.doFinal(ciphertext)) | ||
} catch (e: Exception) { | ||
IterableLogger.e(TAG, "Decryption failed", e) | ||
return encryptedData // Fallback to encrypted data if decryption fails | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters