UNPKG

fast-md5-web

Version:

A TypeScript project with tsup bundler for Rust WASM MD5 calculation

111 lines (97 loc) 4.51 kB
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>详细MD5测试</title> <script src="https://cdn.jsdelivr.net/npm/spark-md5@3.0.2/spark-md5.min.js"></script> </head> <body> <h1>详细MD5测试</h1> <div id="results"></div> <script type="module"> import init, { Md5Calculator } from './wasm_md5.js'; async function test() { await init(); const calculator = new Md5Calculator(); calculator.set_log_enabled(true); const results = document.getElementById('results'); // 测试各种情况 const testCases = [ { name: '空数组', data: new Uint8Array(0), expected: 'd41d8cd98f00b204e9800998ecf8427e' }, { name: '单字节 0x00', data: new Uint8Array([0]), expected: '93b885adfe0da089cdf634904fd59f71' }, { name: '单字节 0x41 (A)', data: new Uint8Array([65]), expected: '7fc56270e7a70fa81a5935b72eacbe29' }, { name: '字符串 "a"', data: new TextEncoder().encode('a'), expected: '0cc175b9c0f1b6a831c399e269772661' }, { name: '字符串 "hello"', data: new TextEncoder().encode('hello'), expected: '5d41402abc4b2a76b9719d911017c592' }, { name: '字符串 "hello world"', data: new TextEncoder().encode('hello world'), expected: '5eb63bbbe01eeed093cb22bb8f5acdc3' }, { name: '重复字节 [1,1,1,1]', data: new Uint8Array([1, 1, 1, 1]), expected: null // 我们会用Spark MD5计算 } ]; for (const testCase of testCases) { console.log(`\n=== 测试: ${testCase.name} ===`); // WASM 计算 console.log('数据:', Array.from(testCase.data)); const wasmResult = await calculator.calculate_md5_async(testCase.data, 32); console.log('WASM 结果:', wasmResult); // Spark MD5 计算 const spark = new SparkMD5.ArrayBuffer(); spark.append(testCase.data.buffer); const sparkResult = spark.end(); console.log('Spark 结果:', sparkResult); // 预期结果 const expected = testCase.expected || sparkResult; console.log('预期结果:', expected); const wasmCorrect = wasmResult === expected; const sparkCorrect = sparkResult === expected; const consistent = wasmResult === sparkResult; console.log('WASM 正确:', wasmCorrect); console.log('Spark 正确:', sparkCorrect); console.log('两者一致:', consistent); results.innerHTML += ` <div style="border: 1px solid #ccc; margin: 10px; padding: 10px;"> <h3>${testCase.name}</h3> <p><strong>数据:</strong> [${Array.from(testCase.data).join(', ')}]</p> <p><strong>WASM:</strong> <code>${wasmResult}</code> ${wasmCorrect ? '✅' : '❌'}</p> <p><strong>Spark:</strong> <code>${sparkResult}</code> ${sparkCorrect ? '✅' : '❌'}</p> <p><strong>预期:</strong> <code>${expected}</code></p> <p><strong>一致性:</strong> ${consistent ? '✅ 一致' : '❌ 不一致'}</p> </div> `; if (!consistent) { console.error(`❌ 不一致: ${testCase.name}`); console.error(`WASM: ${wasmResult}`); console.error(`Spark: ${sparkResult}`); } } console.log('\n=== 测试完成 ==='); } test().catch(console.error); </script> </body> </html>