mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-11 21:30:11 +08:00
Code cleanup. Remove duplicated internal utilities.
This commit is contained in:
parent
e7dcf676fc
commit
3be16c4644
@ -43,7 +43,6 @@ import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl
|
||||
import net.mamoe.mirai.console.internal.logging.MiraiConsoleLogger
|
||||
import net.mamoe.mirai.console.internal.permission.BuiltInPermissionService
|
||||
import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl
|
||||
import net.mamoe.mirai.console.internal.util.autoHexToBytes
|
||||
import net.mamoe.mirai.console.internal.util.runIgnoreException
|
||||
import net.mamoe.mirai.console.logging.LoggerController
|
||||
import net.mamoe.mirai.console.permission.PermissionService
|
||||
@ -285,7 +284,7 @@ internal class MiraiConsoleImplementationBridge(
|
||||
}
|
||||
MD5 -> {
|
||||
val md5 = kotlin.runCatching {
|
||||
account.password.value.autoHexToBytes()
|
||||
account.password.value.hexToBytes()
|
||||
}.getOrElse {
|
||||
error("Bad auto-login md5: '${account.password.value}' for account $id")
|
||||
}
|
||||
|
@ -17,15 +17,16 @@ import net.mamoe.mirai.console.MiraiConsoleImplementation.ConsoleDataScope.Compa
|
||||
import net.mamoe.mirai.console.command.*
|
||||
import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig
|
||||
import net.mamoe.mirai.console.internal.data.builtins.DataScope
|
||||
import net.mamoe.mirai.console.internal.util.autoHexToBytes
|
||||
import net.mamoe.mirai.console.util.scopeWith
|
||||
import net.mamoe.mirai.message.nextMessageOrNull
|
||||
import net.mamoe.mirai.utils.BotConfiguration
|
||||
import net.mamoe.mirai.utils.Either
|
||||
import net.mamoe.mirai.utils.Either.Companion.flatMapNull
|
||||
import net.mamoe.mirai.utils.Either.Companion.fold
|
||||
import net.mamoe.mirai.utils.hexToBytes
|
||||
import net.mamoe.mirai.utils.secondsToMillis
|
||||
|
||||
@Suppress("RESTRICTED_CONSOLE_COMMAND_OWNER") // IDE plugin
|
||||
internal open class LoginCommandImpl : SimpleCommand(
|
||||
ConsoleCommandOwner, "login", "登录",
|
||||
description = "登录一个账号",
|
||||
@ -52,7 +53,7 @@ internal open class LoginCommandImpl : SimpleCommand(
|
||||
val config = DataScope.get<AutoLoginConfig>()
|
||||
val acc = config.accounts.firstOrNull { it.account == id.toString() } ?: return Either.right(null)
|
||||
val strv = acc.password.value
|
||||
return if (acc.password.kind == AutoLoginConfig.Account.PasswordKind.MD5) Either.left(strv.autoHexToBytes()) else Either.right(
|
||||
return if (acc.password.kind == AutoLoginConfig.Account.PasswordKind.MD5) Either.left(strv.hexToBytes()) else Either.right(
|
||||
strv
|
||||
)
|
||||
}
|
||||
|
@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.console.internal.util
|
||||
|
||||
import java.security.MessageDigest
|
||||
|
||||
|
||||
@Suppress("DuplicatedCode") // false positive. `this` is not the same for `List<Byte>` and `ByteArray`
|
||||
internal fun ByteArray.checkOffsetAndLength(offset: Int, length: Int) {
|
||||
require(offset >= 0) { "offset shouldn't be negative: $offset" }
|
||||
require(length >= 0) { "length shouldn't be negative: $length" }
|
||||
require(offset + length <= this.size) { "offset ($offset) + length ($length) > array.size (${this.size})" }
|
||||
}
|
||||
|
||||
internal fun String.autoHexToBytes(): ByteArray =
|
||||
this.trim(Char::isWhitespace).asSequence().chunked(2).map {
|
||||
(it[0].toString() + it[1]).toUByte(16).toByte()
|
||||
}.toList().toByteArray()
|
||||
|
||||
internal fun ByteArray.md5(offset: Int = 0, length: Int = this.size - offset): ByteArray {
|
||||
this.checkOffsetAndLength(offset, length)
|
||||
return MessageDigest.getInstance("MD5").apply { update(this@md5, offset, length) }.digest()
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
@Suppress("DuplicatedCode") // false positive. foreach is not common to UByteArray and ByteArray
|
||||
internal fun ByteArray.toUHexString(
|
||||
separator: String = " ",
|
||||
offset: Int = 0,
|
||||
length: Int = this.size - offset
|
||||
): String {
|
||||
this.checkOffsetAndLength(offset, length)
|
||||
if (length == 0) {
|
||||
return ""
|
||||
}
|
||||
val lastIndex = offset + length
|
||||
return buildString(length * 2) {
|
||||
this@toUHexString.forEachIndexed { index, it ->
|
||||
if (index in offset until lastIndex) {
|
||||
var ret = it.toUByte().toString(16).uppercase()
|
||||
if (ret.length == 1) ret = "0$ret"
|
||||
append(ret)
|
||||
if (index < lastIndex - 1) append(separator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
13
mirai-core-utils/src/commonMain/kotlin/Base64.kt
Normal file
13
mirai-core-utils/src/commonMain/kotlin/Base64.kt
Normal file
@ -0,0 +1,13 @@
|
||||
/*
|
||||
* Copyright 2019-2022 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
public expect fun ByteArray.encodeBase64(): String
|
||||
public expect fun String.decodeBase64(): ByteArray
|
@ -1,10 +1,10 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
* Copyright 2019-2022 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:JvmMultifileClass
|
||||
@ -72,9 +72,6 @@ private fun Byte.fixToString(): String {
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalUnsignedTypes::class)
|
||||
@JvmOverloads
|
||||
@Suppress("DuplicatedCode") // false positive. foreach is not common to UByteArray and ByteArray
|
||||
public fun ByteArray.toUHexString(
|
||||
separator: String = " ",
|
||||
offset: Int = 0,
|
||||
@ -103,60 +100,6 @@ public fun ByteArray.checkOffsetAndLength(offset: Int, length: Int) {
|
||||
require(offset + length <= this.size) { "offset ($offset) + length ($length) > array.size (${this.size})" }
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
@Suppress("DuplicatedCode") // false positive. foreach is not common to UByteArray and ByteArray
|
||||
public fun Array<Byte>.toUHexString(
|
||||
separator: String = " ",
|
||||
offset: Int = 0,
|
||||
length: Int = this.size - offset
|
||||
): String {
|
||||
require(offset >= 0) { "offset shouldn't be negative: $offset" }
|
||||
require(length >= 0) { "length shouldn't be negative: $length" }
|
||||
require(offset + length <= this.size) { "offset ($offset) + length ($length) > array.size (${this.size})" }
|
||||
|
||||
if (length == 0) {
|
||||
return ""
|
||||
}
|
||||
val lastIndex = offset + length
|
||||
return buildString(length * 2) {
|
||||
this@toUHexString.forEachIndexed { index, it ->
|
||||
if (index in offset until lastIndex) {
|
||||
var ret = it.toUByte().toString(16).uppercase()
|
||||
if (ret.length == 1) ret = "0$ret"
|
||||
append(ret)
|
||||
if (index < lastIndex - 1) append(separator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@JvmOverloads
|
||||
@Suppress("DuplicatedCode") // false positive. foreach is not common to UByteArray and ByteArray
|
||||
public fun List<Byte>.toUHexString(separator: String = " ", offset: Int = 0, length: Int = this.size - offset): String {
|
||||
require(offset >= 0) { "offset shouldn't be negative: $offset" }
|
||||
require(length >= 0) { "length shouldn't be negative: $length" }
|
||||
require(offset + length <= this.size) { "offset ($offset) + length ($length) > array.size (${this.size})" }
|
||||
|
||||
if (length == 0) {
|
||||
return ""
|
||||
}
|
||||
val lastIndex = offset + length
|
||||
return buildString(length * 2) {
|
||||
this@toUHexString.forEachIndexed { index, it ->
|
||||
if (index in offset until lastIndex) {
|
||||
var ret = it.toUByte().toString(16).uppercase()
|
||||
if (ret.length == 1) ret = "0$ret"
|
||||
append(ret)
|
||||
if (index < lastIndex - 1) append(separator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@JvmSynthetic
|
||||
@Suppress("DuplicatedCode") // false positive. foreach is not common to UByteArray and ByteArray
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UByteArray.toUHexString(separator: String = " ", offset: Int = 0, length: Int = this.size - offset): String {
|
||||
if (length == 0) {
|
||||
return ""
|
||||
@ -174,9 +117,6 @@ public fun UByteArray.toUHexString(separator: String = " ", offset: Int = 0, len
|
||||
}
|
||||
}
|
||||
|
||||
public expect fun ByteArray.encodeBase64(): String
|
||||
public expect fun String.decodeBase64(): ByteArray
|
||||
|
||||
public inline fun ByteArray.toReadPacket(offset: Int = 0, length: Int = this.size - offset): ByteReadPacket =
|
||||
ByteReadPacket(this, offset = offset, length = length)
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
* Copyright 2019-2022 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
@ -46,7 +46,7 @@ internal class Desensitizer private constructor(
|
||||
}
|
||||
|
||||
fun desensitize(value: Array<Byte>): Array<Byte> {
|
||||
return desensitize(value.toUHexString()).hexToBytes().toTypedArray()
|
||||
return desensitize(value.toByteArray().toUHexString()).hexToBytes().toTypedArray()
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user