Skip to content

Instantly share code, notes, and snippets.

@EmmanuelGuther
Created January 24, 2018 16:42
Show Gist options
  • Save EmmanuelGuther/b25ad5e46a78165a0bbd201d6ec73d65 to your computer and use it in GitHub Desktop.
Save EmmanuelGuther/b25ad5e46a78165a0bbd201d6ec73d65 to your computer and use it in GitHub Desktop.
import android.util.Base64
import java.io.UnsupportedEncodingException
/**
* @param message the message to be encoded
*
* @return the enooded from of the message
*/
fun toBase64(message: String): String? {
val data: ByteArray
try {
data = message.toByteArray(charset("UTF-8"))
return Base64.encodeToString(data, Base64.DEFAULT)
} catch (e: UnsupportedEncodingException) {
e.printStackTrace()
}
return null
}
/**
* @param message the encoded message
*
* @return the decoded message
*/
fun fromBase64(message: String?): String? {
val data = Base64.decode(message, Base64.DEFAULT)
try {
return String(data, charset("UTF-8"))
} catch (e: UnsupportedEncodingException) {
e.printStackTrace()
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment