Compare commits

...
3 Commits
Author SHA1 Message Date
Yi-Cyuan ChenandClaude Opus 5.5 66db5dbd99 ### Fixed
- Incorrect hash for `DataView` input.
- Throws `ArrayBuffer is not defined` in browsers without `ArrayBuffer`.

### Changed
- Multi-byte typed arrays such as `Uint16Array`, `Uint32Array` and `Float32Array` are now hashed by their underlying bytes, the same as Node.js `crypto` and Web Crypto. Previously each element was treated as one byte.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 13:31:55 +08:00
Yi-Cyuan ChenandClaude Opus 5.5 c2a7870840 ### Changed
- Clarify the benchmark summary in the README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 15:14:32 +08:00
Yi-Cyuan ChenandClaude Opus 5.5 698e7154d2 update README badges
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:01:17 +08:00
31 changed files with 193 additions and 108 deletions
+8
View File
@@ -1,5 +1,13 @@
# Change Log
## v0.11.0 / 2026-11-18
### Fixed
- Incorrect hash for `DataView` input.
- Throws `ArrayBuffer is not defined` in browsers without `ArrayBuffer`.
### Changed
- Multi-byte typed arrays such as `Uint16Array`, `Uint32Array` and `Float32Array` are now hashed by their underlying bytes, the same as Node.js `crypto` and Web Crypto. Previously each element was treated as one byte.
## v0.10.0 / 2026-11-16
### Fixed
- Unpaired UTF-16 surrogates produce wrong digests and consume the next code unit in the JS path. Unpaired surrogates are now encoded as U+FFFD, matching `TextEncoder` and Node.js `crypto`. #57
+5 -4
View File
@@ -1,7 +1,8 @@
# js-md5
[![CI](https://github.com/emn178/js-md5/actions/workflows/ci.yml/badge.svg)](https://github.com/emn178/js-md5/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/emn178/js-md5/badge.svg?branch=master)](https://coveralls.io/r/emn178/js-md5?branch=master)
[![NPM](https://nodei.co/npm/js-md5.png?stars&downloads)](https://nodei.co/npm/js-md5/)
[![Coverage Status](https://coveralls.io/repos/emn178/js-md5/badge.svg?branch=master)](https://coveralls.io/r/emn178/js-md5?branch=master)
[![NPM](https://img.shields.io/npm/v/js-md5)](https://www.npmjs.com/package/js-md5)
[![CDNJS](https://img.shields.io/jsdelivr/npm/hm/js-md5)](https://www.jsdelivr.com/package/npm/js-md5)
A simple and fast MD5 hash function for JavaScript supports UTF-8 encoding.
@@ -39,7 +40,7 @@ APIs, including `hex`, `array`, `digest`, and `arrayBuffer` output.
- [Async Benchmark (measurethat.net)](https://measurethat.net/Benchmarks/Show/29176/0/md5-performance-comparison-very-long-text-v6)
- [Async Benchmark (jsperf.app)](https://jsperf.app/jonuhi/9)
* js-md5 is the fastest synchronous MD5 library, but slower than Hash-WASM
* js-md5 is the fastest for short strings in these benchmarks. For large data, WebAssembly-based libraries (e.g. Hash-WASM) are faster.
- [File Benchmark - Issue #19](https://github.com/emn178/js-md5/issues/19)
- [File Benchmark - Issue #42](https://github.com/emn178/js-md5/issues/42)
@@ -102,7 +103,7 @@ md5('The quick brown fox jumps over the lazy dog.'); // e4d909c290d0fb1ca068ffad
// It also supports UTF-8 encoding
md5('中文'); // a7bac2239fcdcb3a067903d8077c4a07
// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` and other `ArrayBuffer` views (their underlying bytes are hashed)
md5([]); // d41d8cd98f00b204e9800998ecf8427e
md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "js-md5",
"version": "0.10.0",
"version": "0.11.0",
"main": ["src/md5.js"],
"ignore": [
"tests"
+10 -4
View File
@@ -2,7 +2,7 @@
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -39,7 +39,7 @@
var isArray = Array.isArray;
var isView = ArrayBuffer.isView;
var isView = ArrayBuffer.isView ;
// [message: string, isString: bool]
var formatMessage = function (message) {
@@ -53,10 +53,14 @@
if (message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
@@ -172,6 +176,8 @@
throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (isView(message) && message.constructor !== Buffer) {
message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
}
}
if (isArray(message) || isView(message) ||
+2 -2
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+8 -4
View File
@@ -8,7 +8,7 @@ var md5$1 = {exports: {}};
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -42,7 +42,7 @@ var md5$1 = {exports: {}};
var isArray = Array.isArray;
var isView = ArrayBuffer.isView;
var isView = ArrayBuffer.isView ;
// [message: string, isString: bool]
var formatMessage = function (message) {
@@ -56,10 +56,14 @@ var md5$1 = {exports: {}};
if (message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
+10 -4
View File
@@ -13,7 +13,7 @@ var md5$1 = {exports: {}};
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -51,7 +51,7 @@ var md5$1 = {exports: {}};
var isArray = Array.isArray;
var isView = ArrayBuffer.isView;
var isView = ArrayBuffer.isView ;
// [message: string, isString: bool]
var formatMessage = function (message) {
@@ -65,10 +65,14 @@ var md5$1 = {exports: {}};
if (message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
@@ -184,6 +188,8 @@ var md5$1 = {exports: {}};
throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (isView(message) && message.constructor !== Buffer) {
message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
}
}
if (isArray(message) || isView(message) ||
+10 -4
View File
@@ -2,7 +2,7 @@
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -58,7 +58,7 @@
};
}
var isView = ArrayBuffer.isView;
var isView = ARRAY_BUFFER ? ArrayBuffer.isView : null;
/* istanbul ignore else */
if (ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) {
@@ -78,10 +78,14 @@
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!ARRAY_BUFFER || !isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
@@ -202,6 +206,8 @@
throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (ARRAY_BUFFER && isView(message) && message.constructor !== Buffer) {
message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
}
}
if (isArray(message) || isView(message) ||
+2 -2
View File
File diff suppressed because one or more lines are too long
+2 -2
View File
File diff suppressed because one or more lines are too long
+8 -4
View File
@@ -8,7 +8,7 @@ var md5$1 = {exports: {}};
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -61,7 +61,7 @@ var md5$1 = {exports: {}};
};
}
var isView = ArrayBuffer.isView;
var isView = ARRAY_BUFFER ? ArrayBuffer.isView : null;
/* istanbul ignore else */
if (ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) {
@@ -81,10 +81,14 @@ var md5$1 = {exports: {}};
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!ARRAY_BUFFER || !isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
+10 -4
View File
@@ -2,7 +2,7 @@
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -49,7 +49,7 @@
var isArray = Array.isArray;
var isView = ArrayBuffer.isView;
var isView = ArrayBuffer.isView ;
// [message: string, isString: bool]
var formatMessage = function (message) {
@@ -63,10 +63,14 @@
if (message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
@@ -182,6 +186,8 @@
throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (isView(message) && message.constructor !== Buffer) {
message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
}
}
if (isArray(message) || isView(message) ||
+2 -2
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+8 -4
View File
@@ -8,7 +8,7 @@ var md5$1 = {exports: {}};
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -52,7 +52,7 @@ var md5$1 = {exports: {}};
var isArray = Array.isArray;
var isView = ArrayBuffer.isView;
var isView = ArrayBuffer.isView ;
// [message: string, isString: bool]
var formatMessage = function (message) {
@@ -66,10 +66,14 @@ var md5$1 = {exports: {}};
if (message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
+10 -4
View File
@@ -13,7 +13,7 @@ var md5$1 = {exports: {}};
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -61,7 +61,7 @@ var md5$1 = {exports: {}};
var isArray = Array.isArray;
var isView = ArrayBuffer.isView;
var isView = ArrayBuffer.isView ;
// [message: string, isString: bool]
var formatMessage = function (message) {
@@ -75,10 +75,14 @@ var md5$1 = {exports: {}};
if (message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
@@ -194,6 +198,8 @@ var md5$1 = {exports: {}};
throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (isView(message) && message.constructor !== Buffer) {
message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
}
}
if (isArray(message) || isView(message) ||
+10 -4
View File
@@ -13,7 +13,7 @@ var md5$1 = {exports: {}};
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -70,7 +70,7 @@ var md5$1 = {exports: {}};
};
}
var isView = ArrayBuffer.isView;
var isView = ARRAY_BUFFER ? ArrayBuffer.isView : null;
/* istanbul ignore else */
if (ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) {
@@ -90,10 +90,14 @@ var md5$1 = {exports: {}};
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!ARRAY_BUFFER || !isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
@@ -214,6 +218,8 @@ var md5$1 = {exports: {}};
throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (ARRAY_BUFFER && isView(message) && message.constructor !== Buffer) {
message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
}
}
if (isArray(message) || isView(message) ||
+9 -9
View File
@@ -93,7 +93,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line815">line 815</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line821">line 821</a>
</li></ul></dd>
@@ -224,7 +224,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line734">line 734</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line740">line 740</a>
</li></ul></dd>
@@ -351,7 +351,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line746">line 746</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line752">line 752</a>
</li></ul></dd>
@@ -478,7 +478,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line784">line 784</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line790">line 790</a>
</li></ul></dd>
@@ -607,7 +607,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line768">line 768</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line774">line 774</a>
</li></ul></dd>
@@ -734,7 +734,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line712">line 712</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line718">line 718</a>
</li></ul></dd>
@@ -861,7 +861,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line667">line 667</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line673">line 673</a>
</li></ul></dd>
@@ -988,7 +988,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line700">line 700</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line706">line 706</a>
</li></ul></dd>
@@ -1173,7 +1173,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line363">line 363</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line369">line 369</a>
</li></ul></dd>
+9 -9
View File
@@ -47,7 +47,7 @@
<dt class="tag-version">Version:</dt>
<dd class="tag-version"><ul class="dummy"><li>0.10.0</li></ul></dd>
<dd class="tag-version"><ul class="dummy"><li>0.11.0</li></ul></dd>
@@ -244,7 +244,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line115">line 115</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line119">line 119</a>
</li></ul></dd>
@@ -417,7 +417,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line124">line 124</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line128">line 128</a>
</li></ul></dd>
@@ -590,7 +590,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line143">line 143</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line147">line 147</a>
</li></ul></dd>
@@ -765,7 +765,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line133">line 133</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line137">line 137</a>
</li></ul></dd>
@@ -880,7 +880,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line158">line 158</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line162">line 162</a>
</li></ul></dd>
@@ -1053,7 +1053,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line106">line 106</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line110">line 110</a>
</li></ul></dd>
@@ -1226,7 +1226,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line95">line 95</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line99">line 99</a>
</li></ul></dd>
@@ -1401,7 +1401,7 @@ md5('The quick brown fox jumps over the lazy dog');</code></pre>
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line166">line 166</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line170">line 170</a>
</li></ul></dd>
+1 -1
View File
@@ -214,7 +214,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line900">line 900</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line906">line 906</a>
</li></ul></dd>
+5 -4
View File
@@ -45,8 +45,9 @@
<section>
<article><h1>js-md5</h1>
<p><a href="https://github.com/emn178/js-md5/actions/workflows/ci.yml"><img src="https://github.com/emn178/js-md5/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
<a href="https://coveralls.io/r/emn178/js-md5?branch=master"><img src="https://coveralls.io/repos/emn178/js-md5/badge.svg?branch=master" alt="Coverage Status"></a><br>
<a href="https://nodei.co/npm/js-md5/"><img src="https://nodei.co/npm/js-md5.png?stars&amp;downloads" alt="NPM"></a></p>
<a href="https://coveralls.io/r/emn178/js-md5?branch=master"><img src="https://coveralls.io/repos/emn178/js-md5/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="https://www.npmjs.com/package/js-md5"><img src="https://img.shields.io/npm/v/js-md5" alt="NPM"></a>
<a href="https://www.jsdelivr.com/package/npm/js-md5"><img src="https://img.shields.io/jsdelivr/npm/hm/js-md5" alt="CDNJS"></a></p>
<p>A simple and fast MD5 hash function for JavaScript supports UTF-8 encoding.</p>
<h2>Demo</h2>
<p><a href="http://emn178.github.io/online-tools/md5.html">MD5 Online</a><br>
@@ -114,7 +115,7 @@ APIs, including <code>hex</code>, <code>array</code>, <code>digest</code>, and <
<li><a href="https://jsperf.app/jonuhi/9">Async Benchmark (jsperf.app)</a></li>
</ul>
<ul>
<li>js-md5 is the fastest synchronous MD5 library, but slower than Hash-WASM</li>
<li>js-md5 is the fastest for short strings in these benchmarks. For large data, WebAssembly-based libraries (e.g. Hash-WASM) are faster.</li>
</ul>
<ul>
<li><a href="https://github.com/emn178/js-md5/issues/19">File Benchmark - Issue #19</a></li>
@@ -166,7 +167,7 @@ md5('The quick brown fox jumps over the lazy dog.'); // e4d909c290d0fb1ca068ffad
// It also supports UTF-8 encoding
md5('中文'); // a7bac2239fcdcb3a067903d8077c4a07
// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` and other `ArrayBuffer` views (their underlying bytes are hashed)
md5([]); // d41d8cd98f00b204e9800998ecf8427e
md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e
+9 -9
View File
@@ -71,7 +71,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line225">line 225</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line231">line 231</a>
</li></ul></dd>
@@ -252,7 +252,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line251">line 251</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line257">line 257</a>
</li></ul></dd>
@@ -457,7 +457,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line261">line 261</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line267">line 267</a>
</li></ul></dd>
@@ -662,7 +662,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line282">line 282</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line288">line 288</a>
</li></ul></dd>
@@ -869,7 +869,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line271">line 271</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line277">line 277</a>
</li></ul></dd>
@@ -1042,7 +1042,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line298">line 298</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line304">line 304</a>
</li></ul></dd>
@@ -1247,7 +1247,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line241">line 241</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line247">line 247</a>
</li></ul></dd>
@@ -1452,7 +1452,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line228">line 228</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line234">line 234</a>
</li></ul></dd>
@@ -1659,7 +1659,7 @@ md5.hmac('key', 'The quick brown fox jumps over the lazy dog');</code></pre>
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line307">line 307</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line313">line 313</a>
</li></ul></dd>
+9 -9
View File
@@ -93,7 +93,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line335">line 335</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line341">line 341</a>
</li></ul></dd>
@@ -208,7 +208,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line734">line 734</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line740">line 740</a>
</li></ul></dd>
@@ -330,7 +330,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line746">line 746</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line752">line 752</a>
</li></ul></dd>
@@ -452,7 +452,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line784">line 784</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line790">line 790</a>
</li></ul></dd>
@@ -576,7 +576,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line768">line 768</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line774">line 774</a>
</li></ul></dd>
@@ -698,7 +698,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line712">line 712</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line718">line 718</a>
</li></ul></dd>
@@ -820,7 +820,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line667">line 667</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line673">line 673</a>
</li></ul></dd>
@@ -942,7 +942,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line700">line 700</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line706">line 706</a>
</li></ul></dd>
@@ -1122,7 +1122,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line363">line 363</a>
<a href="md5.js.html">md5.js</a>, <a href="md5.js.html#line369">line 369</a>
</li></ul></dd>
+11 -5
View File
@@ -30,7 +30,7 @@
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -94,7 +94,7 @@
};
}
var isView = ArrayBuffer.isView;
var isView = ARRAY_BUFFER ? ArrayBuffer.isView : null;
/* istanbul ignore else */
if (FEATURE_LEGACY &amp;&amp; ARRAY_BUFFER &amp;&amp; (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) {
@@ -114,11 +114,15 @@
if (ARRAY_BUFFER &amp;&amp; message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) &amp;&amp; !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!ARRAY_BUFFER || !isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
}
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
* @method hex
@@ -238,6 +242,8 @@
throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (ARRAY_BUFFER &amp;&amp; isView(message) &amp;&amp; message.constructor !== Buffer) {
message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
}
}
if (isArray(message) || isView(message) ||
Vendored
+1 -1
View File
@@ -1,4 +1,4 @@
type Message = string | number[] | ArrayBuffer | Uint8Array;
type Message = string | number[] | ArrayBuffer | ArrayBufferView;
interface Hasher {
/**
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "js-md5",
"version": "0.10.0",
"version": "0.11.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "js-md5",
"version": "0.10.0",
"version": "0.11.0",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-commonjs": "^29.0.3",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "js-md5",
"version": "0.10.0",
"version": "0.11.0",
"description": "A simple MD5 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/md5.js",
"module": "build/md5.mjs",
+11 -5
View File
@@ -2,7 +2,7 @@
* [js-md5]{@link https://github.com/emn178/js-md5}
*
* @namespace md5
* @version 0.10.0
* @version 0.11.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -66,7 +66,7 @@
};
}
var isView = ArrayBuffer.isView;
var isView = ARRAY_BUFFER ? ArrayBuffer.isView : null;
/* istanbul ignore else */
if (FEATURE_LEGACY && ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) {
@@ -86,11 +86,15 @@
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
}
if (!isArray(message) && !isView(message)) {
if (isArray(message)) {
return [message, false];
}
if (!ARRAY_BUFFER || !isView(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false];
}
// read the underlying bytes of any view (DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
/**
* @method hex
@@ -210,6 +214,8 @@
throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
} else if (ARRAY_BUFFER && isView(message) && message.constructor !== Buffer) {
message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
}
}
if (isArray(message) || isView(message) ||
+10
View File
@@ -57,4 +57,14 @@ expect(browserLite.base64).to.be(undefined);
expect(browserLite.buffer).to.be(undefined);
expect(browserLite.hmac).to.be(undefined);
// legacy builds load in browsers without ArrayBuffer
['build/md5.js', 'build/md5.min.js'].forEach(function (file) {
var context = vm.createContext({});
vm.runInContext('var window = this; delete ArrayBuffer; delete Uint8Array; delete Uint32Array; delete DataView;', context);
vm.runInContext(fs.readFileSync(file, 'utf8'), context);
expect(context.md5('abc')).to.be(EXPECTED_MD5);
expect(context.md5.create().update('a').update([98, 99]).hex()).to.be(EXPECTED_MD5);
expect(context.md5.hmac('key', 'abc')).to.be(EXPECTED_HMAC);
});
console.log('Build variants: PASS');
+5
View File
@@ -99,6 +99,11 @@
testCases['Int8Array'] = {
'9e107d9d372bb6826bd81d3542a419d6': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
};
testCases['ArrayBuffer view'] = {
'71f3ccb98530e3072e2eaef01da20d8c': new Int8Array([-1, -128, 65, 0, -2]),
'050d144172d916d0846f839e0412e929': new Uint16Array(new Uint8Array([2, 1]).buffer),
'eb61eead90e3b899c6bcbe27ac581660': new DataView(new Uint8Array([9, 72, 69, 76, 76, 79, 9]).buffer, 1, 5)
};
testCases['ArrayBuffer'] = {
'93b885adfe0da089cdf634904fd59f71': new ArrayBuffer(1)
};