diff --git a/scalabot-app/src/main/java/net/lamgc/scalabot/util/ByteUtils.java b/scalabot-app/src/main/java/net/lamgc/scalabot/util/ByteUtils.java deleted file mode 100644 index 3888fdf..0000000 --- a/scalabot-app/src/main/java/net/lamgc/scalabot/util/ByteUtils.java +++ /dev/null @@ -1,17 +0,0 @@ -package net.lamgc.scalabot.util; - -final class ByteUtils { - - private ByteUtils() { - } - - public static String bytesToHexString(byte[] bytes) { - StringBuilder builder = new StringBuilder(); - for (byte aByte : bytes) { - String hexBit = Integer.toHexString(aByte & 0xFF); - builder.append(hexBit.length() == 1 ? "0" + hexBit : hexBit); - } - return builder.toString(); - } - -} diff --git a/scalabot-app/src/main/kotlin/BotDBMaker.kt b/scalabot-app/src/main/kotlin/BotDBMaker.kt index 09f8ca2..bdea555 100644 --- a/scalabot-app/src/main/kotlin/BotDBMaker.kt +++ b/scalabot-app/src/main/kotlin/BotDBMaker.kt @@ -2,7 +2,7 @@ package net.lamgc.scalabot import com.google.common.io.Files import mu.KotlinLogging -import net.lamgc.scalabot.util.toHaxString +import net.lamgc.scalabot.util.toHexString import org.mapdb.DB import org.mapdb.DBException import org.mapdb.DBMaker @@ -186,5 +186,5 @@ private object BotAccountIdDbAdapter : FileDbAdapter("BotAccountId", { botAccoun private object BotTokenDbAdapter : FileDbAdapter("BotToken_v0.1.0", { botAccount -> val digest: MessageDigest = MessageDigest.getInstance("SHA-256") val digestBytes = digest.digest(botAccount.token.toByteArray(StandardCharsets.UTF_8)) - File(AppPaths.DATA_DB.file, "${digestBytes.toHaxString()}.db") + File(AppPaths.DATA_DB.file, "${digestBytes.toHexString()}.db") }) \ No newline at end of file diff --git a/scalabot-app/src/main/kotlin/util/Utils.kt b/scalabot-app/src/main/kotlin/util/Utils.kt index 5504f1d..0d74fd0 100644 --- a/scalabot-app/src/main/kotlin/util/Utils.kt +++ b/scalabot-app/src/main/kotlin/util/Utils.kt @@ -9,7 +9,7 @@ import java.io.FileFilter import java.io.FilenameFilter import java.net.URL -internal fun ByteArray.toHaxString(): String = ByteUtils.bytesToHexString(this) +internal fun ByteArray.toHexString(): String = joinToString("") { it.toString(16) } internal fun Artifact.equalsArtifact(that: Artifact): Boolean = this.groupId.equals(that.groupId) && diff --git a/scalabot-app/src/test/kotlin/util/UtilsKtTest.kt b/scalabot-app/src/test/kotlin/util/UtilsKtTest.kt index 84e0fbb..d9934a1 100644 --- a/scalabot-app/src/test/kotlin/util/UtilsKtTest.kt +++ b/scalabot-app/src/test/kotlin/util/UtilsKtTest.kt @@ -1,9 +1,9 @@ package net.lamgc.scalabot.util import org.eclipse.aether.artifact.DefaultArtifact -import org.junit.jupiter.api.Assertions.assertFalse -import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test +import java.nio.charset.StandardCharsets internal class UtilsKtTest { @@ -16,4 +16,10 @@ internal class UtilsKtTest { .equalsArtifact(DefaultArtifact("com.example:demo-2:1.0.0-SNAPSHOT")) ) } + + @Test + fun `bytes to hex`() { + assertEquals("48656c6c6f20576f726c64", "Hello World".toByteArray(StandardCharsets.UTF_8).toHexString()) + } + } \ No newline at end of file