pto.0.1.0.source-code.Utils.kt Maven / Gradle / Ivy
package se.wollan.crypto
import java.util.Base64
internal fun String.fromBase64(): ByteArray = Base64.getDecoder().decode(this)
internal fun ByteArray.toBase64(): String = Base64.getEncoder().encodeToString(this)
internal fun ByteArray.toStringUTF8(): String = toString(Charsets.UTF_8)
@OptIn(ExperimentalStdlibApi::class)
internal fun ByteArray.toHexString(): String = toHexString(HexFormat.Default)
@OptIn(ExperimentalStdlibApi::class)
internal fun String.fromHexString(): ByteArray = hexToByteArray(HexFormat.Default)
internal fun ByteArray.xor(second: ByteArray): ByteArray {
require(size == second.size) { "Arrays must have the same length, $size != ${second.size}." }
val result = ByteArray(size)
for (i in indices) {
result[i] = (this[i].toInt() xor second[i].toInt()).toByte()
}
return result
}