fast-md5-web
Version:
A TypeScript project with tsup bundler for Rust WASM MD5 calculation
75 lines (64 loc) • 3.21 kB
HTML
<html>
<head>
<title>Console MD5 Test</title>
</head>
<body>
<h1>MD5 Console Test</h1>
<p>Check browser console for results</p>
<script type="module">
import init, { Md5Calculator } from './wasm_md5.js';
// Load SparkMD5 via script tag
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/spark-md5@3.0.2/spark-md5.min.js';
document.head.appendChild(script);
// Wait for SparkMD5 to load
await new Promise(resolve => {
script.onload = resolve;
});
async function runTests() {
try {
await init();
const calculator = new Md5Calculator();
console.log('=== MD5 Test Results ===');
// Test 1: Empty data
const emptyData = new Uint8Array(0);
const wasmEmpty = calculator.calculate_md5_async(emptyData, 32);
const sparkEmpty = SparkMD5.ArrayBuffer.hash(emptyData.buffer);
console.log('Empty data:');
console.log(' WASM:', wasmEmpty);
console.log(' Spark:', sparkEmpty);
console.log(' Match:', wasmEmpty === sparkEmpty);
// Test 2: "hello" string
const helloData = new TextEncoder().encode('hello');
const wasmHello = calculator.calculate_md5_async(helloData, 32);
const sparkHello = SparkMD5.ArrayBuffer.hash(helloData.buffer);
console.log('\n"hello" string:');
console.log(' WASM:', wasmHello);
console.log(' Spark:', sparkHello);
console.log(' Expected: 5d41402abc4b2a76b9719d911017c592');
console.log(' WASM Match:', wasmHello === '5d41402abc4b2a76b9719d911017c592');
console.log(' Spark Match:', sparkHello === '5d41402abc4b2a76b9719d911017c592');
// Test 3: Single byte 'A'
const aData = new Uint8Array([65]); // 'A'
const wasmA = calculator.calculate_md5_async(aData, 32);
const sparkA = SparkMD5.ArrayBuffer.hash(aData.buffer);
console.log('\nSingle byte \'A\':');
console.log(' WASM:', wasmA);
console.log(' Spark:', sparkA);
console.log(' Expected: 7fc56270e7a70fa81a5935b72eacbe29');
console.log(' WASM Match:', wasmA === '7fc56270e7a70fa81a5935b72eacbe29');
console.log(' Spark Match:', sparkA === '7fc56270e7a70fa81a5935b72eacbe29');
// Test 4: Data inspection
console.log('\n=== Data Inspection ===');
console.log('Empty data length:', emptyData.length);
console.log('Hello data:', Array.from(helloData));
console.log('A data:', Array.from(aData));
} catch (error) {
console.error('Test failed:', error);
}
}
runTests();
</script>
</body>
</html>