Skip to content

Commit

Permalink
Allow large (but safe) integers for counters.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Apr 15, 2017
1 parent 8ac229a commit 583860c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,14 @@
throw new Error('invalid counter value (must be an integer)');
}

// We cannot safely handle numbers beyond the safe range for integers
if (value > Number.MAX_SAFE_INTEGER) {
throw new Error('integer value out of safe range');
}

for (var index = 15; index >= 0; --index) {
this._counter[index] = value % 256;
value = value >> 8;
value = parseInt(value / 256);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aes-js",
"version": "3.0.0",
"version": "3.1.0",
"bugs": {
"url": "http://github.com/ricmoo/aes-js/issues",
"email": "github@ricmoo.com"
Expand Down
2 changes: 2 additions & 0 deletions test/test-counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ module.exports = {
'test-counter-number-254': makeTest({number: 254, incrementResult: "000000000000000000000000000000ff"}),
'test-counter-number-255': makeTest({number: 255, incrementResult: "00000000000000000000000000000100"}),
'test-counter-number-256': makeTest({number: 256, incrementResult: "00000000000000000000000000000101"}),
'test-counter-number-large': makeTest({number: 1099511627774, incrementResult: "0000000000000000000000ffffffffff"}),
'test-counter-number-max': makeTest({number: 9007199254740991, incrementResult: "00000000000000000020000000000000"}),
'test-counter-bytes-0000': makeTest({bytes: "00000000000000000000000000000000", incrementResult: "00000000000000000000000000000001"}),
'test-counter-bytes-00ff': makeTest({bytes: "000000000000000000000000000000ff", incrementResult: "00000000000000000000000000000100"}),
'test-counter-bytes-ffff': makeTest({bytes: "ffffffffffffffffffffffffffffffff", incrementResult: "00000000000000000000000000000000"}),
Expand Down
7 changes: 7 additions & 0 deletions test/test-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ module.exports = {
},
'invalid counter setValue (non-integer number) failed to throw an error');

test.throws(function() {
var counter = new aes.Counter(Number.MAX_SAFE_INTEGER + 1);
}, function(error) {
return (error.message === 'integer value out of safe range');
},
'invalid counter value (out of range) failed to throw an error');

var badThings = [0, 1.5, 1];
for (var i = 0; i < badThings.length; i++) {
var counter = new aes.Counter();
Expand Down

0 comments on commit 583860c

Please sign in to comment.