Greasy Fork

WbiSign

2025/4/17 11:16:50

目前为 2025-04-17 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.icu/scripts/533087/1572455/WbiSign.js

// ==UserScript==
// @name        WbiSign
// @namespace   Violentmonkey Scripts
// @version     1.0
// @author      -
// @description 2025/4/17 11:16:50
// @require     https://unpkg.com/[email protected]/spark-md5.min.js
// ==/UserScript==
class WbiSign {
  constructor() {
    this.mixinKeyEncTab = [
      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, 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, 57,
      62, 11, 36, 20, 34, 44, 52,
    ];
  }
  // 对 imgKey 和 subKey 进行字符顺序打乱编码
  getMixinKey = (orig) =>
    this.mixinKeyEncTab
      .map((n) => orig[n])
      .join("")
      .slice(0, 32);

  // 为请求参数进行 wbi 签名
  encWbi(params, img_key, sub_key) {
    const mixin_key = this.getMixinKey(img_key + sub_key),
      curr_time = Math.round(Date.now() / 1000),
      chr_filter = /[!'()*]/g;

    Object.assign(params, { wts: curr_time }); // 添加 wts 字段
    // 按照 key 重排参数
    const query = Object.keys(params)
      .sort()
      .map((key) => {
        // 过滤 value 中的 "!'()*" 字符
        const value = params[key].toString().replace(chr_filter, "");
        return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
      })
      .join("&");

    const wbi_sign = SparkMD5.hash(query + mixin_key); // 计算 w_rid

    return query + "&w_rid=" + wbi_sign;
  }
}