diff --git a/packages/update/src/UpdateSDK.ts b/packages/update/src/UpdateSDK.ts index 5c1c2a1..950eb98 100644 --- a/packages/update/src/UpdateSDK.ts +++ b/packages/update/src/UpdateSDK.ts @@ -2,6 +2,7 @@ import AsyncStorage from '@react-native-async-storage/async-storage' import { Linking, Platform } from 'react-native' import { apiRequest, getConfig, getUserId, _registerUserInfoHandler } from '@xuqm/rn-common' import { getAppVersionCode, getAppVersionName, _devSetAppVersion } from './NativeVersion' +import { md5 } from './md5' // ─── Types (public) ──────────────────────────────────────────────────────────── @@ -343,6 +344,9 @@ export const UpdateSDK = { /** * Android APK 直接下载并调起系统安装器。 + * + * 当前实现使用 Linking.openURL 打开下载链接,由浏览器完成下载和安装引导。 + * 如需应用内下载 + 静默安装,宿主可集成 react-native-blob-util 并自行实现。 */ async downloadAndInstallApk( downloadUrl: string, @@ -352,15 +356,7 @@ export const UpdateSDK = { }, ): Promise { if (Platform.OS !== 'android') throw new Error('[UpdateSDK] APK install is Android-only.') - // NativeApkInstall 由宿主提供(TurboModule),若未注入则 fallback 到 Linking - try { - // eslint-disable-next-line @typescript-eslint/no-require-imports - const { XuqmApkInstall } = require('./NativeApkInstall') as { XuqmApkInstall: { installApk(url: string, sha256?: string | null): Promise } } - await XuqmApkInstall.installApk(downloadUrl, options?.sha256 ?? null) - } catch { - // Native module not available — fallback to Linking - await Linking.openURL(downloadUrl) - } + await Linking.openURL(downloadUrl) }, // ── 插件(Bundle)热更新 ────────────────────────────────────────────────── @@ -429,6 +425,16 @@ export const UpdateSDK = { const source = await _downloadText(info.downloadUrl, options?.onProgress) + // MD5 校验 + if (info.md5) { + const actual = md5(source) + if (actual !== info.md5) { + throw new Error( + `[UpdateSDK] Bundle MD5 mismatch for "${moduleId}": expected ${info.md5}, got ${actual}`, + ) + } + } + // 写入 AsyncStorage 缓存(版本记录) const cachePayload: CachedRnBundle = { moduleId, diff --git a/packages/update/src/md5.ts b/packages/update/src/md5.ts new file mode 100644 index 0000000..4467fc1 --- /dev/null +++ b/packages/update/src/md5.ts @@ -0,0 +1,127 @@ +/** + * Minimal MD5 implementation for bundle integrity verification. + * Based on the RSA Data Security, Inc. MD5 Message-Digest Algorithm (RFC 1321). + */ + +function _ff(a: number, b: number, c: number, d: number, x: number, s: number, ac: number): number { + a = (a + ((b & c) | (~b & d)) + x + ac) | 0 + return (((a << s) | (a >>> (32 - s))) + b) | 0 +} + +function _gg(a: number, b: number, c: number, d: number, x: number, s: number, ac: number): number { + a = (a + ((b & d) | (c & ~d)) + x + ac) | 0 + return (((a << s) | (a >>> (32 - s))) + b) | 0 +} + +function _hh(a: number, b: number, c: number, d: number, x: number, s: number, ac: number): number { + a = (a + (b ^ c ^ d) + x + ac) | 0 + return (((a << s) | (a >>> (32 - s))) + b) | 0 +} + +function _ii(a: number, b: number, c: number, d: number, x: number, s: number, ac: number): number { + a = (a + (c ^ (b | ~d)) + x + ac) | 0 + return (((a << s) | (a >>> (32 - s))) + b) | 0 +} + +function _toWords(input: string): number[] { + const msg = unescape(encodeURIComponent(input)) + const len = msg.length + const n = (((len + 8) >>> 6) << 4) + 16 + const words = new Array(n).fill(0) + for (let i = 0; i < len; i++) { + words[i >> 2] |= (msg.charCodeAt(i) & 0xff) << ((i % 4) << 3) + } + words[len >> 2] |= 0x80 << ((len % 4) << 3) + words[n - 2] = len << 3 + return words +} + +function _hex(n: number): string { + let s = '' + for (let j = 0; j < 4; j++) { + s += '0123456789abcdef'.charAt((n >> (j * 8 + 4)) & 0x0f) + + '0123456789abcdef'.charAt((n >> (j * 8)) & 0x0f) + } + return s +} + +export function md5(input: string): string { + const x = _toWords(input) + let a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476 + + for (let i = 0; i < x.length; i += 16) { + const oa = a, ob = b, oc = c, od = d + + a = _ff(a, b, c, d, x[i], 7, 0xd76aa478) + d = _ff(d, a, b, c, x[i + 1], 12, 0xe8c7b756) + c = _ff(c, d, a, b, x[i + 2], 17, 0x242070db) + b = _ff(b, c, d, a, x[i + 3], 22, 0xc1bdceee) + a = _ff(a, b, c, d, x[i + 4], 7, 0xf57c0faf) + d = _ff(d, a, b, c, x[i + 5], 12, 0x4787c62a) + c = _ff(c, d, a, b, x[i + 6], 17, 0xa8304613) + b = _ff(b, c, d, a, x[i + 7], 22, 0xfd469501) + a = _ff(a, b, c, d, x[i + 8], 7, 0x698098d8) + d = _ff(d, a, b, c, x[i + 9], 12, 0x8b44f7af) + c = _ff(c, d, a, b, x[i + 10],17, 0xffff5bb1) + b = _ff(b, c, d, a, x[i + 11],22, 0x895cd7be) + a = _ff(a, b, c, d, x[i + 12], 7, 0x6b901122) + d = _ff(d, a, b, c, x[i + 13],12, 0xfd987193) + c = _ff(c, d, a, b, x[i + 14],17, 0xa679438e) + b = _ff(b, c, d, a, x[i + 15],22, 0x49b40821) + + a = _gg(a, b, c, d, x[i + 1], 5, 0xf61e2562) + d = _gg(d, a, b, c, x[i + 6], 9, 0xc040b340) + c = _gg(c, d, a, b, x[i + 11],14, 0x265e5a51) + b = _gg(b, c, d, a, x[i], 20, 0xe9b6c7aa) + a = _gg(a, b, c, d, x[i + 5], 5, 0xd62f105d) + d = _gg(d, a, b, c, x[i + 10], 9, 0x02441453) + c = _gg(c, d, a, b, x[i + 15],14, 0xd8a1e681) + b = _gg(b, c, d, a, x[i + 4], 20, 0xe7d3fbc8) + a = _gg(a, b, c, d, x[i + 9], 5, 0x21e1cde6) + d = _gg(d, a, b, c, x[i + 14], 9, 0xc33707d6) + c = _gg(c, d, a, b, x[i + 3], 14, 0xf4d50d87) + b = _gg(b, c, d, a, x[i + 8], 20, 0x455a14ed) + a = _gg(a, b, c, d, x[i + 13], 5, 0xa9e3e905) + d = _gg(d, a, b, c, x[i + 2], 9, 0xfcefa3f8) + c = _gg(c, d, a, b, x[i + 7], 14, 0x676f02d9) + b = _gg(b, c, d, a, x[i + 12],20, 0x8d2a4c8a) + + a = _hh(a, b, c, d, x[i + 5], 4, 0xfffa3942) + d = _hh(d, a, b, c, x[i + 8], 11, 0x8771f681) + c = _hh(c, d, a, b, x[i + 11],16, 0x6d9d6122) + b = _hh(b, c, d, a, x[i + 14],23, 0xfde5380c) + a = _hh(a, b, c, d, x[i + 1], 4, 0xa4beea44) + d = _hh(d, a, b, c, x[i + 4], 11, 0x4bdecfa9) + c = _hh(c, d, a, b, x[i + 7], 16, 0xf6bb4b60) + b = _hh(b, c, d, a, x[i + 10],23, 0xbebfbc70) + a = _hh(a, b, c, d, x[i + 13], 4, 0x289b7ec6) + d = _hh(d, a, b, c, x[i], 11, 0xeaa127fa) + c = _hh(c, d, a, b, x[i + 3], 16, 0xd4ef3085) + b = _hh(b, c, d, a, x[i + 6], 23, 0x04881d05) + a = _hh(a, b, c, d, x[i + 9], 4, 0xd9d4d039) + d = _hh(d, a, b, c, x[i + 12],11, 0xe6db99e5) + c = _hh(c, d, a, b, x[i + 15],16, 0x1fa27cf8) + b = _hh(b, c, d, a, x[i + 2], 23, 0xc4ac5665) + + a = _ii(a, b, c, d, x[i], 6, 0xf4292244) + d = _ii(d, a, b, c, x[i + 7], 10, 0x432aff97) + c = _ii(c, d, a, b, x[i + 14],15, 0xab9423a7) + b = _ii(b, c, d, a, x[i + 5], 21, 0xfc93a039) + a = _ii(a, b, c, d, x[i + 12], 6, 0x655b59c3) + d = _ii(d, a, b, c, x[i + 3], 10, 0x8f0ccc92) + c = _ii(c, d, a, b, x[i + 10],15, 0xffeff47d) + b = _ii(b, c, d, a, x[i + 1], 21, 0x85845dd1) + a = _ii(a, b, c, d, x[i + 8], 6, 0x6fa87e4f) + d = _ii(d, a, b, c, x[i + 15],10, 0xfe2ce6e0) + c = _ii(c, d, a, b, x[i + 6], 15, 0xa3014314) + b = _ii(b, c, d, a, x[i + 13],21, 0x4e0811a1) + a = _ii(a, b, c, d, x[i + 4], 6, 0xf7537e82) + d = _ii(d, a, b, c, x[i + 11],10, 0xbd3af235) + c = _ii(c, d, a, b, x[i + 2], 15, 0x2ad7d2bb) + b = _ii(b, c, d, a, x[i + 9], 21, 0xeb86d391) + + a = (a + oa) | 0; b = (b + ob) | 0; c = (c + oc) | 0; d = (d + od) | 0 + } + + return _hex(a) + _hex(b) + _hex(c) + _hex(d) +}