aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/Base64Util.kt
blob: 0b7b3eac2f4595b3722c50c098fb70f1a16d09bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package moe.nea.firmament.util

import java.util.Base64

object Base64Util {
	fun decodeString(str: String): String {
		return decodeBytes(str).decodeToString()
	}

	fun decodeBytes(str: String): ByteArray {
		return Base64.getDecoder().decode(str.padToValidBase64())
	}

	fun String.padToValidBase64(): String {
		val align = this.length % 4
		if (align == 0) return this
		return this + "=".repeat(4 - align)
	}

	fun encodeToString(bytes: ByteArray): String {
		return Base64.getEncoder().encodeToString(bytes)
	}
}