From 04db18b253f62d9ef88203b64560693b33812868 Mon Sep 17 00:00:00 2001 From: Artiom Tretjakovas <44021713+ArtiomTr@users.noreply.github.com> Date: Fri, 18 Feb 2022 17:08:15 +0200 Subject: [PATCH] Javascript unsigned signed typed arrays return same hash (#114) * Created failing test * Fixed bug --- index.js | 6 +++--- test/index.js | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 657aff2..962bf62 100644 --- a/index.js +++ b/index.js @@ -348,7 +348,7 @@ function typeHasher(options, writeTo, context){ return this.dispatch(Array.prototype.slice.call(arr)); }, _int8array: function(arr){ - write('uint8array:'); + write('int8array:'); return this.dispatch(Array.prototype.slice.call(arr)); }, _uint16array: function(arr){ @@ -356,7 +356,7 @@ function typeHasher(options, writeTo, context){ return this.dispatch(Array.prototype.slice.call(arr)); }, _int16array: function(arr){ - write('uint16array:'); + write('int16array:'); return this.dispatch(Array.prototype.slice.call(arr)); }, _uint32array: function(arr){ @@ -364,7 +364,7 @@ function typeHasher(options, writeTo, context){ return this.dispatch(Array.prototype.slice.call(arr)); }, _int32array: function(arr){ - write('uint32array:'); + write('int32array:'); return this.dispatch(Array.prototype.slice.call(arr)); }, _float32array: function(arr){ diff --git a/test/index.js b/test/index.js index 5f46a69..b5092af 100644 --- a/test/index.js +++ b/test/index.js @@ -323,4 +323,12 @@ describe('hash', function() { assert.equal(ha, hb, 'Hashing should not respect the order of Set entries'); }); } + + if (typeof Uint8Array !== 'undefined') { + it('respects typed array\'s types', function () { + assert.notEqual(hash(new Int8Array([1,2,3,4])), hash(new Uint8Array([1,2,3,4])), 'Hashing should respect signed / unsigned Int8Array type'); + assert.notEqual(hash(new Int16Array([1,2,3,4])), hash(new Uint16Array([1,2,3,4])), 'Hashing should respect signed / unsigned Int16Array type'); + assert.notEqual(hash(new Int32Array([1,2,3,4])), hash(new Uint32Array([1,2,3,4])), 'Hashing should respect signed / unsigned Int32Array type'); + }); + } });