Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base 64 Encoding Decoding for iOS in Multiplatform Kotlin

I am working on an iOS app, which uses multiplatform Kotlin. I need to encode/decode a string into base64. I am able to encode a normal string with below code, but I am not able to decode a base 64 string into normal string. Below is my code.

fun encodeToBase64()  {
    var st: NSString = "normalString"
    var data: NSData? = st.dataUsingEncoding(encoding = 
    NSUTF8StringEncoding)
    if (data != null) {
        var str = data.base64EncodedStringWithOptions(options = 0)
        println("base 64 string == $str")
    }
}

Thanks

like image 746
Sundeep Saluja Avatar asked Nov 16 '22 06:11

Sundeep Saluja


1 Answers

For anyone still looking for base64 encoding and decoding and/or hashing in multiplatform projects. The Okio library has multiplatform support and can be used as a kotlin native solution. (https://square.github.io/okio/multiplatform/)

fun shar256(input: String): String? = input.encodeUtf8().sha256()

 fun base64Encoded(input: String): String? = input.encodeUtf8().base64()

 fun base64Decoded(input: String): String? = input.decodeBase64()?.utf8()
like image 72
Huib Avatar answered Dec 29 '22 01:12

Huib