给Wbi签名算法的Demo添加C#实现 (#719)
Co-authored-by: 社会易姐QwQ <45892418+SocialSisterYi@users.noreply.github.com>
This commit is contained in:
parent
a8742859fc
commit
63deb66605
@ -115,7 +115,7 @@
|
|||||||
|
|
||||||
## Wbi签名算法实现Demo
|
## Wbi签名算法实现Demo
|
||||||
|
|
||||||
该 Demo 提供 [Python](#Python)、[JavaScript](#JavaScript)、[Golang](#Golang) 语言
|
该 Demo 提供 [Python](#Python)、[JavaScript](#JavaScript)、[Golang](#Golang)、[C#](#CSharp) 语言
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
@ -422,60 +422,100 @@ func main() {
|
|||||||
fmt.Println(string(body))
|
fmt.Println(string(body))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
### Java
|
|
||||||
|
|
||||||
需要 `hutool` 依赖
|
### CSharp
|
||||||
|
|
||||||
```java
|
无需依赖外部库
|
||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import cn.hutool.crypto.SecureUtil;
|
```cs
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
import java.util.*;
|
class Program
|
||||||
|
{
|
||||||
|
private static HttpClient _httpClient = new();
|
||||||
|
|
||||||
public class WbiTest {
|
private static readonly int[] MixinKeyEncTab =
|
||||||
private static final int[] mixinKeyEncTab = new int[]{
|
{
|
||||||
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49,
|
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, 33, 9, 42, 19, 29, 28, 14, 39,
|
||||||
33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40,
|
12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40, 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63,
|
||||||
61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11,
|
57, 62, 11, 36, 20, 34, 44, 52
|
||||||
36, 20, 34, 44, 52
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public static String getMixinKey(String imgKey, String subKey) {
|
//对 imgKey 和 subKey 进行字符顺序打乱编码
|
||||||
String s = imgKey + subKey;
|
private static string GetMixinKey(string orig)
|
||||||
StringBuilder key = new StringBuilder();
|
{
|
||||||
for (int i = 0; i < 32; i++) {
|
return MixinKeyEncTab.Aggregate("", (s, i) => s + orig[i])[..32];
|
||||||
key.append(s.charAt(mixinKeyEncTab[i]));
|
|
||||||
}
|
|
||||||
return key.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
private static Dictionary<string, string> EncWbi(Dictionary<string, string> parameters, string imgKey,
|
||||||
String imgKey = "653657f524a547ac981ded72ea172057";
|
string subKey)
|
||||||
String subKey = "6e4909c702f846728e64f6007736a338";
|
{
|
||||||
String mixinKey = getMixinKey(imgKey, subKey);
|
string mixinKey = GetMixinKey(imgKey + subKey);
|
||||||
System.out.println(mixinKey);
|
string currTime = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
|
||||||
//72136226c6a73669787ee4fd02a74c27
|
//添加 wts 字段
|
||||||
//{
|
parameters["wts"] = currTime;
|
||||||
// foo: '114',
|
// 按照 key 重排参数
|
||||||
// bar: '514',
|
parameters = parameters.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value);
|
||||||
// zab: 1919810
|
//过滤 value 中的 "!'()*" 字符
|
||||||
//}
|
parameters = parameters.ToDictionary(
|
||||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
kvp => kvp.Key,
|
||||||
map.put("foo", "114");
|
kvp => new string(kvp.Value.Where(chr => !"!'()*".Contains(chr)).ToArray())
|
||||||
map.put("bar", "514");
|
);
|
||||||
map.put("zab", 1919810);
|
// 序列化参数
|
||||||
map.put("wts", System.currentTimeMillis() / 1000);
|
string query = new FormUrlEncodedContent(parameters).ReadAsStringAsync().Result;
|
||||||
StringJoiner param = new StringJoiner("&");
|
//计算 w_rid
|
||||||
//排序 + 拼接字符串
|
using MD5 md5 = MD5.Create();
|
||||||
map.entrySet().stream()
|
byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(query + mixinKey));
|
||||||
.sorted(Map.Entry.comparingByKey())
|
string wbiSign = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
|
||||||
.forEach(entry -> param.add(entry.getKey() + "=" + entry.getValue().toString()));
|
parameters["w_rid"] = wbiSign;
|
||||||
String s = param + mixinKey;
|
|
||||||
String wbiSign = SecureUtil.md5(s);
|
return parameters;
|
||||||
System.out.println(wbiSign);
|
}
|
||||||
String finalParam = param + "&w_rid=" + wbiSign;
|
|
||||||
System.out.println(finalParam);
|
// 获取最新的 img_key 和 sub_key
|
||||||
|
private static async Task<(string, string)> GetWbiKeys()
|
||||||
|
{
|
||||||
|
HttpResponseMessage responseMessage = await _httpClient.SendAsync(new HttpRequestMessage
|
||||||
|
{
|
||||||
|
Method = HttpMethod.Get,
|
||||||
|
RequestUri = new Uri("https://api.bilibili.com/x/web-interface/nav"),
|
||||||
|
});
|
||||||
|
|
||||||
|
JsonNode response = JsonNode.Parse(await responseMessage.Content.ReadAsStringAsync())!;
|
||||||
|
|
||||||
|
string imgUrl = (string)response["data"]!["wbi_img"]!["img_url"]!;
|
||||||
|
imgUrl = imgUrl.Split("/")[^1].Split(".")[0];
|
||||||
|
|
||||||
|
string subUrl = (string)response["data"]!["wbi_img"]!["sub_url"]!;
|
||||||
|
subUrl = subUrl.Split("/")[^1].Split(".")[0];
|
||||||
|
return (imgUrl, subUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task Main()
|
||||||
|
{
|
||||||
|
var (imgKey, subKey) = await GetWbiKeys();
|
||||||
|
|
||||||
|
Dictionary<string, string> signedParams = EncWbi(
|
||||||
|
parameters: new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "foo", "114" },
|
||||||
|
{ "bar", "514" },
|
||||||
|
{ "baz", "1919810" }
|
||||||
|
},
|
||||||
|
imgKey: imgKey,
|
||||||
|
subKey: subKey
|
||||||
|
);
|
||||||
|
|
||||||
|
string query = await new FormUrlEncodedContent(signedParams).ReadAsStringAsync();
|
||||||
|
|
||||||
|
Console.WriteLine(query);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
输出内容为进行 Wbi 签名的后参数的 url query 形式
|
||||||
|
|
||||||
|
```
|
||||||
|
bar=514&baz=1919810&foo=114&wts=1687541921&w_rid=26e82b1b9b3a11dbb1807a9228a40d3b
|
||||||
```
|
```
|
||||||
Loading…
Reference in New Issue
Block a user