feat: enhance message encryption and media file conversion with improved compression and base64 encoding
This commit is contained in:
@@ -762,10 +762,14 @@ object MessageCrypto {
|
||||
val deflater = java.util.zip.Deflater()
|
||||
deflater.setInput(replyJson.toByteArray(Charsets.UTF_8))
|
||||
deflater.finish()
|
||||
val compressedBuffer = ByteArray(replyJson.length * 2 + 100)
|
||||
val compressedSize = deflater.deflate(compressedBuffer)
|
||||
val outputStream = java.io.ByteArrayOutputStream()
|
||||
val buffer = ByteArray(8192)
|
||||
while (!deflater.finished()) {
|
||||
val count = deflater.deflate(buffer)
|
||||
outputStream.write(buffer, 0, count)
|
||||
}
|
||||
deflater.end()
|
||||
val compressed = compressedBuffer.copyOf(compressedSize)
|
||||
val compressed = outputStream.toByteArray()
|
||||
|
||||
// PBKDF2 key derivation (matching crypto-js: crypto.PBKDF2(password, 'rosetta', {keySize: 256/32, iterations: 1000}))
|
||||
// Используем generatePBKDF2Key() для совместимости с crypto-js (UTF-8 encoding)
|
||||
@@ -789,8 +793,8 @@ object MessageCrypto {
|
||||
|
||||
result
|
||||
} catch (e: Exception) {
|
||||
// Fallback: return plaintext (for backwards compatibility)
|
||||
replyJson
|
||||
android.util.Log.e("MessageCrypto", "encryptReplyBlob failed", e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -165,15 +165,16 @@ object MediaUtils {
|
||||
*/
|
||||
suspend fun uriToBase64File(context: Context, uri: Uri): String? = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
|
||||
val inputStream = context.contentResolver.openInputStream(uri)
|
||||
|
||||
val inputStream = context.contentResolver.openInputStream(uri)
|
||||
?: return@withContext null
|
||||
|
||||
|
||||
val bytes = inputStream.readBytes()
|
||||
inputStream.close()
|
||||
|
||||
val base64 = Base64.encodeToString(bytes, Base64.NO_WRAP)
|
||||
|
||||
|
||||
val mimeType = context.contentResolver.getType(uri) ?: "application/octet-stream"
|
||||
val base64 = "data:$mimeType;base64," + Base64.encodeToString(bytes, Base64.NO_WRAP)
|
||||
|
||||
base64
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
|
||||
Reference in New Issue
Block a user