Skip to content

Commit

Permalink
fix bug in unsignedView() and signedView() extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Skycoder42 committed Nov 20, 2024
1 parent 54a5153 commit a45fb14
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
2 changes: 2 additions & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ scripts:
packageFilters:
flutter: false
exec: >-
dart pub remove collection &&
dart pub add 'dev:flutter:{"sdk":"flutter"}' &&
dart pub upgrade --major-versions --tighten &&
dart pub remove flutter &&
dart pub add 'dev:collection' &&
dart pub upgrade
upgrade:flutter:
Expand Down
8 changes: 8 additions & 0 deletions packages/sodium/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.4.2] - 2024-11-20
### Changed
- Updated dependencies

### Fixed
- Fixed wrong invalid buffer offset for `signedView()` and `unsignedView()`

## [3.4.1] - 2024-11-07
### Changed
- Improved performance by removing unnecessary byte array copies for output data
Expand Down Expand Up @@ -255,6 +262,7 @@ changed, only the name of the getter. (#61)
### Added
- Initial stable release

[3.4.2]: https://github.com/Skycoder42/libsodium_dart_bindings/compare/sodium-v3.4.1...sodium-v3.4.2
[3.4.1]: https://github.com/Skycoder42/libsodium_dart_bindings/compare/sodium-v3.4.0...sodium-v3.4.1
[3.4.0]: https://github.com/Skycoder42/libsodium_dart_bindings/compare/sodium-v3.3.0...sodium-v3.4.0
[3.3.0]: https://github.com/Skycoder42/libsodium_dart_bindings/compare/sodium-v3.2.0+1...sodium-v3.3.0
Expand Down
6 changes: 4 additions & 2 deletions packages/sodium/lib/src/api/string_x.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ extension Int8ListX on Int8List {
/// Casts this signed byte array to an unsigned [Uint8List].
///
/// The returned list referres the the same underlying data.
Uint8List unsignedView() => Uint8List.view(buffer);
// ignore: use_to_and_as_if_applicable
Uint8List unsignedView() => Uint8List.sublistView(this);
}

/// Extensions on [Uint8List]
extension Uint8ListX on Uint8List {
/// Casts this unsigned byte array to a signed [Int8List].
///
/// The returned list referres the the same underlying data.
Int8List signedView() => Int8List.view(buffer);
// ignore: use_to_and_as_if_applicable
Int8List signedView() => Int8List.sublistView(this);
}
2 changes: 1 addition & 1 deletion packages/sodium/lib/src/js/api/sumo/pwhash_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class PwhashJS with PwHashValidations implements Pwhash {
final result = jsErrorWrap(
() => sodium.crypto_pwhash(
outLen,
Uint8List.view(password.buffer).toJS,
password.unsignedView().toJS,
salt.toJS,
opsLimit,
memLimit,
Expand Down
8 changes: 4 additions & 4 deletions packages/sodium/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sodium
description: Dart bindings for libsodium, for the Dart-VM and for the Web
version: 3.4.1
version: 3.4.2
homepage: https://github.com/Skycoder42/libsodium_dart_bindings

environment:
Expand All @@ -17,11 +17,11 @@ dev_dependencies:
cider: ^0.2.8
code_builder: ^4.10.1
collection: ^1.19.1
coverage: ^1.10.0
coverage: ^1.11.0
custom_lint: ^0.6.8
dart_pre_commit: ^5.4.0
dart_test_tools: ^6.0.0
ffigen: ^15.0.0
ffigen: ^16.0.0
freezed: ^2.5.7
mocktail: ^1.0.4
path: ^1.9.1
Expand All @@ -36,7 +36,7 @@ ffigen:
// ignore_for_file: unused_field
headers:
entry-points:
- "/workspaces/libsodium_dart_bindings/packages/sodium/test/integration/binaries/linux/include/sodium.h"
- "test/integration/binaries/linux/include/sodium.h"
compiler-opts:
- "-I/usr/lib/clang/18/include/"
exclude-all-by-default: true
Expand Down
58 changes: 40 additions & 18 deletions packages/sodium/test/unit/api/string_x_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,55 @@ void main() {
});
});

test('unsignedView returns a buffer view', () {
const testData = [1, 2, 3];
group('unsignedView', () {
test('returns a buffer view', () {
const testData = [-1, 0, 1];

final sut = Int8List.fromList(testData);
final view = sut.unsignedView();
final sut = Int8List.fromList(testData);
final view = sut.unsignedView();

expect(view, testData);
expect(view, const [255, 0, 1]);

sut[0] = 10;
expect(view[0], 10);
sut[0] = 10;
expect(view[0], 10);

view[1] = 20;
expect(sut[1], 20);
view[1] = 20;
expect(sut[1], 20);
});

test('retains subviews on the original data', () {
final testData = List.generate(100, (i) => i);

final sut = Int8List.view(Int8List.fromList(testData).buffer, 25, 20);
final view = sut.unsignedView();

expect(view, testData.sublist(25, 45));
});
});

test('signedView returns a buffer view', () {
const testData = [1, 2, 3];
group('signedView', () {
test('returns a buffer view', () {
const testData = [127, 128, 129];

final sut = Uint8List.fromList(testData);
final view = sut.signedView();
final sut = Uint8List.fromList(testData);
final view = sut.signedView();

expect(view, testData);
expect(view, const [127, -128, -127]);

sut[0] = 10;
expect(view[0], 10);
sut[0] = 10;
expect(view[0], 10);

view[1] = 20;
expect(sut[1], 20);
view[1] = 20;
expect(sut[1], 20);
});

test('retains subviews on the original data', () {
final testData = List.generate(100, (i) => i);

final sut = Uint8List.view(Uint8List.fromList(testData).buffer, 25, 20);
final view = sut.signedView();

expect(view, testData.sublist(25, 45));
});
});
}

0 comments on commit a45fb14

Please sign in to comment.