Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Replace right shift with truncating divide (#128)
Browse files Browse the repository at this point in the history
Closes #125

In JavaScript the shift operators cause numbers to be truncated to 32
bits, so the `>> 32` operation will always result in 0 on the web. This
causes a problem once the length of the file hits 512MB.
  • Loading branch information
natebosch authored Apr 18, 2022
1 parent 3bae716 commit 4297d24
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 3.0.2-dev

- Require Dart 2.14.0.
* Require Dart 2.14.0.
* Fix bug calculating hashes for content larger than 512MB when compiled to JS.

## 3.0.1

Expand Down
2 changes: 1 addition & 1 deletion lib/src/hash_sink.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ abstract class HashSink implements Sink<List<int>> {
// We're essentially doing byteData.setUint64(offset, lengthInBits, _endian)
// here, but that method isn't supported on dart2js so we implement it
// manually instead.
var highBits = lengthInBits >> 32;
var highBits = lengthInBits ~/ 0x100000000; // >> 32
var lowBits = lengthInBits & mask32;
if (_endian == Endian.big) {
byteData.setUint32(offset, highBits, _endian);
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ dependencies:
typed_data: ^1.3.0

dev_dependencies:
convert: ^3.0.0
lints: ^1.0.0
test: ^1.16.0
14 changes: 14 additions & 0 deletions test/sha1_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';
import 'dart:convert';

import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -44,6 +45,19 @@ void main() {
});
}
});
group('large file', () {
final chunk = List.filled(1024, 0);
test('produces correct hash', () async {
final sink = AccumulatorSink<Digest>();
final hash = sha1.startChunkedConversion(sink);
for (var i = 0; i < 512 * 1024; i++) {
hash.add(chunk);
}
hash.close();
expect(sink.events.single.toString(),
'5b088492c9f4778f409b7ae61477dec124c99033');
});
});
}

// Standard test vectors from:
Expand Down

0 comments on commit 4297d24

Please sign in to comment.