Get started with md5-wasm CDN

MIT licensed

Generates MD5 hashes in browsers with md5-wasm WebAssembly library.

Tags:
  • md5
  • fast
  • promise
  • async
  • javascript
  • WebAssembly

Stable version

Copied!

How to start using md5-wasm CDN


async function computeMD5(buffer) {
  const wasmFile = await fetch('https://cdn.cdnhub.io/md5-wasm/2.0.0/md5-wasm.js');
  const wasmCode = await wasmFile.arrayBuffer();

  const arrayBufferView = new Uint8Array(wasmCode);
  const wasmModule = await WebAssembly.compile(new WebAssembly.Module(arrayBufferView));
  const wasmInstance = await WebAssembly.instantiate(wasmModule);

  const md5 = wasmInstance.instance.exports.MD5;
  const view = new Uint8Array(new ArrayBuffer(buffer.length));
  view.set(new Uint8Array(buffer));

  const result = await new Promise((resolve) => {
    md5.start();
    md5.update(view);
    md5.digest((arrayBuffer) => {
      const viewResult = new Uint8Array(arrayBuffer);
      resolve(viewResult.buffer);
    });
  });

  const viewResult = new Uint8Array(result);
  const hexString = Array.prototype.map.call(viewResult, (byte) => {
    const hex = byte.toString(16);
    return hex.length === 1 ? '0' + hex : hex;
  }).join('');

  console.log('MD5:', hexString);

  wasmInstance.exports.MD5.destroy();
}

// Usage
const buffer = new Uint8Array([0x01, 0x02, 0x03, 0x04]);
computeMD5(buffer);
Copied!
Copied!

All versions