### 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>
This commit is contained in:
Yi-Cyuan Chen
2026-11-18 13:31:55 +08:00
co-authored by Claude Opus 5.5
parent c2a7870840
commit 66db5dbd99
31 changed files with 186 additions and 103 deletions
+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) ||