Skip to content

Commit

Permalink
fix: 优化Kotlin样例 (#1096)
Browse files Browse the repository at this point in the history
优化点:
- 用实体类保存数据, 缓存/加签更方便
- 之前的貌似有bug (可能是参数顺序导致的? 待确认), 现在这个版本实测无误
  • Loading branch information
MukjepScarlet authored Sep 18, 2024
1 parent 17434e6 commit 1ab70f7
Showing 1 changed file with 59 additions and 44 deletions.
103 changes: 59 additions & 44 deletions docs/misc/sign/wbi.md
Original file line number Diff line number Diff line change
Expand Up @@ -731,24 +731,19 @@ public class WbiTest {

### Kotlin

```kotlin
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
说明: 为了便于使用和缓存, 重新编写为实体类形式, 并拆分了多个文件. 使用官方的JSON序列化. (可以根据需要换成其他的)

private val hexDigits = "0123456789abcdef".toCharArray()

fun ByteArray.toHexString() = buildString(this.size shl 1) {
this@toHexString.forEach { byte ->
append(hexDigits[byte.toInt() ushr 4 and 15])
append(hexDigits[byte.toInt() and 15])
}
}
WbiParams.kt

fun String.toMD5(): String {
val md = MessageDigest.getInstance("MD5")
val digest = md.digest(this.toByteArray())
return digest.toHexString()
```kotlin
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonPrimitive

private fun JsonElement?.get(): String {
check(this != null) { "No contents found" }
return this.jsonPrimitive.content.split('/').last().removeSuffix(".png")
}

private val mixinKeyEncTab = intArrayOf(
Expand All @@ -758,46 +753,66 @@ private val mixinKeyEncTab = intArrayOf(
36, 20, 34, 44, 52
)

fun getMixinKey(imgKey: String, subKey: String): String {
val s = imgKey + subKey
return buildString {
repeat(32) {
append(s[mixinKeyEncTab[it]])
@Serializable
data class WbiParams(
val imgKey: String,
val subKey: String,
) {
// 此处整合了切分参数(直接传入{img_url:string, sub_url:string}即可), 不需要可以删掉
constructor(wbiImg: JsonObject) : this(wbiImg["img_url"].get(), wbiImg["sub_url"].get())

private val mixinKey: String
get() = (imgKey + subKey).let { s ->
buildString {
repeat(32) {
append(s[mixinKeyEncTab[it]])
}
}
}

// 创建对象(GET获取或者读缓存, 比如Redis)之后, 直接调用此函数处理
fun enc(params: Map<String, Any?>): String {
val sorted = params.filterValues { it != null }.toSortedMap()
return buildString {
append(sorted.toQueryString())
val wts = System.currentTimeMillis() / 1000
sorted["wts"] = wts
append("&wts=")
append(wts)
append("&w_rid=")
append((sorted.toQueryString() + mixinKey).toMD5())
}
}
}
```

fun Any.encodeURIComponent() =
URLEncoder.encode(this.toString(), StandardCharsets.UTF_8).replace("+", "%20")
Extensions.kt

fun encWbi(params: Map<String, Any>, imgKey: String, subKey: String): String {
val mixinKey = getMixinKey(imgKey, subKey)
val s = params.toSortedMap().let {
it["wts"] to System.currentTimeMillis() / 1000
it.entries.joinToString("&") { (k, v) ->
"${k.encodeURIComponent()}=${v.encodeURIComponent()}"
}
```kotlin
import java.security.MessageDigest

private val hexDigits = "0123456789abcdef".toCharArray()

fun ByteArray.toHexString() = buildString(this.size shl 1) {
this@toHexString.forEach { byte ->
append(hexDigits[byte.toInt() ushr 4 and 15])
append(hexDigits[byte.toInt() and 15])
}
return "$s&w_rid=${(s + mixinKey).toMD5()}"
}

fun main() {
val imgKey = "653657f524a547ac981ded72ea172057"
val subKey = "6e4909c702f846728e64f6007736a338"
val mixinKey = getMixinKey(imgKey, subKey)
println(mixinKey) // 72136226c6a73669787ee4fd02a74c27

// 需要加签的参数
val param = mapOf(
"foo" to "one+one four",
"bar" to "五一四",
"baz" to 1919810,
)
fun String.toMD5(): String {
val md = MessageDigest.getInstance("MD5")
val digest = md.digest(this.toByteArray())
return digest.toHexString()
}

println(encWbi(param, imgKey, subKey))
fun Map<String, Any?>.toQueryString() = this.filterValues { it != null }.entries.joinToString("&") { (k, v) ->
"${k.encodeURIComponent()}=${v!!.encodeURIComponent()}"
}
```

获取和使用案例略

### PHP

来自[SocialSisterYi/bilibili-API-collect#813](https://github.com/SocialSisterYi/bilibili-API-collect/issues/813)
Expand Down

0 comments on commit 1ab70f7

Please sign in to comment.