JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
16.32k stars 1.18k forks source link

Can I used algorithm triple des in compose mutilplatform ? #4470

Closed thanhhoai162963 closed 8 months ago

thanhhoai162963 commented 8 months ago

I try convert triple des from swift code to kotlin mutilplaform but result outputData?.base64EncodedStringWithOptions(options = 0uL = "SpE3b1aZ7MM=") wrong.

In module iosMain:

`@Suppress("CAST_NEVER_SUCCEEDS") fun String.nsdata(): NSData? { return (this as NSString).dataUsingEncoding(NSUTF8StringEncoding) }

fun String.toUTF8String(): String { val bytes = this.toByteArray(Charsets.UTF_8) return bytes.decodeToString() }

@OptIn(ExperimentalEncodingApi::class) actual fun tripleDesEncryptString(input: String, keyRd: String): String? { val inputData = input.toUTF8String().nsdata() val keyData = keyRd.nsdata() val outLength = cValue<ULongVarOf>() val outputData = NSMutableData.dataWithLength(inputData?.length?.plus(kCCBlockSize3DES) ?: 0uL) val outLength1 = cValue()

memScoped {
    val result = CCCrypt(
        op = kCCEncrypt,
        alg = kCCAlgorithm3DES,
        options = (kCCOptionPKCS7Padding or kCCOptionECBMode),
        key = keyData?.bytes,
        keyLength = keyData?.length ?: 0uL,
        iv = null,
        dataIn = inputData?.bytes,
        dataInLength = inputData?.length ?: 0uL,
        dataOut = outputData?.mutableBytes,
        dataOutAvailable = outputData?.length ?: 0uL,
        dataOutMoved = outLength1.ptr
    )

    if (result != kCCSuccess) {
        return null
    }
    outputData?.setLength(outLength1.size.toULong())
}
return outputData?.base64EncodedStringWithOptions(options = 0uL)

} ` Has anyone ever come across this problem? Hoping for your help

eymar commented 8 months ago

Unless I miss something, the problem is not really related to Compose Multiplatform. You likely need to carefully go through your code and check what parts get "translated" from swift to kotlin not properly.

For example I see:

@Suppress("CAST_NEVER_SUCCEEDS")
fun String.nsdata(): NSData? {
return (this as NSString).dataUsingEncoding(NSUTF8StringEncoding)
}

If you aim to have a multiplatform solution, then you can't use platform specific types in common code. In this case it's NSData. Kotlin's ByteArray can be an alternative, I think.

If it's not common code, then casting String to NSString, afaik, is incorrect. IDE highlighted that issue, but you suppressed it for some reason - @Suppress("CAST_NEVER_SUCCEEDS")

eymar commented 8 months ago

Closing this as unrelated to Compose Multiplatform.

okushnikov commented 3 months ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.