feat: Update PBKDF2 key derivation to use custom UTF-8 implementation for compatibility with crypto-js

This commit is contained in:
k1ngsterr1
2026-01-26 23:30:31 +05:00
parent 0d1903691f
commit b3873a759f

View File

@@ -736,8 +736,9 @@ object MessageCrypto {
// which replaces invalid UTF-8 sequences with U+FFFD // which replaces invalid UTF-8 sequences with U+FFFD
val password = bytesToJsUtf8String(plainKeyAndNonce) val password = bytesToJsUtf8String(plainKeyAndNonce)
// Compress with pako (deflate) // Compress with raw deflate (NO zlib header) - matching pako.deflate()
val deflater = java.util.zip.Deflater() // CRITICAL: nowrap=true for raw deflate (pako default)
val deflater = java.util.zip.Deflater(java.util.zip.Deflater.DEFAULT_COMPRESSION, true)
deflater.setInput(replyJson.toByteArray(Charsets.UTF_8)) deflater.setInput(replyJson.toByteArray(Charsets.UTF_8))
deflater.finish() deflater.finish()
val compressedBuffer = ByteArray(replyJson.length * 2 + 100) val compressedBuffer = ByteArray(replyJson.length * 2 + 100)
@@ -745,17 +746,10 @@ object MessageCrypto {
deflater.end() deflater.end()
val compressed = compressedBuffer.copyOf(compressedSize) val compressed = compressedBuffer.copyOf(compressedSize)
// PBKDF2 key derivation (matching RN: crypto.PBKDF2(password, 'rosetta', {keySize: 256/32, iterations: 1000})) // PBKDF2 key derivation (matching crypto-js)
// CRITICAL: Must use SHA256 to match React Native (not SHA1!) // CRITICAL: Use our custom implementation that properly handles UTF-8!
val factory = javax.crypto.SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256") // Java's PBEKeySpec uses UTF-16, but crypto-js uses UTF-8
val spec = javax.crypto.spec.PBEKeySpec( val keyBytes = generatePBKDF2Key(password)
password.toCharArray(),
"rosetta".toByteArray(Charsets.UTF_8),
1000,
256
)
val secretKey = factory.generateSecret(spec)
val keyBytes = secretKey.encoded
// Generate random IV (16 bytes) // Generate random IV (16 bytes)
val iv = ByteArray(16) val iv = ByteArray(16)