### 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
+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)
};