Files
moonsource-fanqie/tests/fanqie/reading_crypto_spec.lua
T
GarfieldandCopilot 9190db9019 ✨ 初始提交:月读第三方书源插件集合
每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。

Co-authored-by: Copilot <[email protected]>
2026-11-16 12:08:03 +08:00

59 lines
2.5 KiB
Lua

--[[--
番茄 reading crypto 向量(离线)。
@module tests.moonsource_fanqie.reading_crypto_spec
--]]
local Assert = require("support.assert")
-- 设备上 z_stream / inflate 可能已被其他模块以不同签名声明,解压不能因此失效。
pcall(require("ffi").cdef, [[
typedef struct z_stream_s { void *p; } z_stream;
int inflate(z_stream *, int);
]])
package.loaded["moonsource_fanqie.reading.crypto"] = nil
package.loaded["crypto.aes"] = nil
local Aes = require("crypto.aes")
local Crypto = require("moonsource_fanqie.reading.crypto")
local key = Aes.from_hex(Crypto.HARDCODED_KEY_HEX)
local iv = "1234567890123456"
local pt = Aes.from_hex("2b5dbca76c4902000000000000000000")
local ct = Aes.cbc_encrypt(pt, key, iv, true)
Assert.eq(Aes.to_hex(ct), "64fd9775e48333728345235490bca65492c8c2ccd7832564732fb54839593fe2")
local content = Crypto.build_register_content(643680972856619, 0, "1234567890123456")
Assert.eq(content, "MTIzNDU2Nzg5MDEyMzQ1NmT9l3XkgzNyg0UjVJC8plSSyMLM14MlZHMvtUg5WT/i")
-- 19 位 device_id 串超过 2^53:小端 8 字节必须逐位精确
do
local b64 = Crypto.build_register_content("7342475219212192830", 0, "1234567890123456")
local raw = require("utils.text").base64Decode(b64)
local plain_id = Aes.cbc_decrypt(raw:sub(17), key, raw:sub(1, 16), true)
Assert.eq(Aes.to_hex(plain_id:sub(1, 8)), "3e4c404d4fb6e565")
Assert.eq(Crypto.build_register_content("643680972856619", 0, "1234567890123456"), content)
end
-- decrypt_server_key roundtrip
local session = Aes.from_hex("00112233445566778899aabbccddeeff")
local iv2 = "0123456789abcdef"
local blob = require("utils.text").base64Encode(iv2 .. Aes.cbc_encrypt(session, key, iv2, true))
Assert.eq(Crypto.decrypt_server_key(blob), "00112233445566778899AABBCCDDEEFF")
local q = Crypto.build_batch_full_query({
install_id = "1", device_id = "2", version_code = "73733", version_name = "7.3.7.33",
}, "7342475219212192830", { "7463695105224884760" }, 1000)
local qs = Crypto.encode_query(q)
Assert.matches(qs, "item_ids=7463695105224884760")
Assert.is_true(not qs:find("%%2C", 1, true))
Assert.matches(qs, "book_id=7342475219212192830")
local plain = string.rep("番茄正文", 20)
Assert.eq(Crypto.maybe_decompress(Aes.from_hex(
"1f8b08000000000002ff7b3e75d58b9e96676b173f9bd6fe7c04b00161f60cc9f0000000"), 1), plain)
Assert.eq(Crypto.maybe_decompress(Aes.from_hex(
"789c7b3e75d58b9e96676b173f9bd6fe7c04b001a6cea6cd"), 1), plain)
Assert.eq(Crypto.maybe_decompress("plain", 0), "plain")