-
Notifications
You must be signed in to change notification settings - Fork 2
/
Base64.kt
77 lines (64 loc) · 2.68 KB
/
Base64.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package dev.inmo.micro_utils.crypto
import kotlin.experimental.and
private const val BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
private const val BASE64_MASK: Byte = 0x3f
private const val BASE64_PAD = '='
private val BASE64_INVERSE_ALPHABET = IntArray(256) {
BASE64_ALPHABET.indexOf(it.toChar())
}
internal fun Int.toBase64(): Char = BASE64_ALPHABET[this]
internal fun Byte.fromBase64(): Byte = BASE64_INVERSE_ALPHABET[toInt() and 0xff].toByte() and BASE64_MASK
typealias EncodedBase64String = String
typealias EncodedByteArray = ByteArray
fun SourceString.encodeBase64String(): EncodedBase64String = encodeToByteArray().encodeBase64String()
fun SourceString.encodeBase64(): EncodedByteArray = encodeToByteArray().encodeBase64()
fun SourceBytes.encodeBase64String(): EncodedBase64String = buildString {
var i = 0
while (this@encodeBase64String.size > i) {
val read = kotlin.math.min(3, this@encodeBase64String.size - i)
val data = ByteArray(3) {
if (it < read) {
this@encodeBase64String[it + i]
} else {
0
}
}
val padSize = (data.size - read) * 8 / 6
val chunk = ((data[0].toInt() and 0xFF) shl 16) or
((data[1].toInt() and 0xFF) shl 8) or
(data[2].toInt() and 0xFF)
for (index in data.size downTo padSize) {
val char = (chunk shr (6 * index)) and BASE64_MASK.toInt()
append(char.toBase64())
}
repeat(padSize) { append(BASE64_PAD) }
i += read
}
}
fun SourceBytes.encodeBase64(): EncodedByteArray = encodeBase64String().encodeToByteArray()
fun EncodedBase64String.decodeBase64(): SourceBytes = dropLastWhile { it == BASE64_PAD }.encodeToByteArray().decodeBase64()
fun EncodedBase64String.decodeBase64String(): SourceString = decodeBase64().decodeToString()
fun EncodedByteArray.decodeBase64(): SourceBytes {
val result = mutableListOf<Byte>()
val data = ByteArray(4)
(0 until size step 4).forEach { i ->
var read = 0
for (j in 0 until 4) {
if (j + i < size) {
data[j] = get(j + i)
read++
} else {
break
}
}
val chunk = data.foldIndexed(0) { index, result, current ->
result or (current.fromBase64().toInt() shl ((3 - index) * 6))
}
for (index in data.size - 2 downTo (data.size - read)) {
val origin = (chunk shr (8 * index)) and 0xff
result.add(origin.toByte())
}
}
return result.toByteArray()
}
fun EncodedByteArray.decodeBase64String(): SourceString = decodeBase64().decodeToString()