Skip to content

Commit

Permalink
Convert all asUint...List to Uint...List.view
Browse files Browse the repository at this point in the history
  • Loading branch information
dipu-bd committed Jun 29, 2024
1 parent d133153 commit 1129d26
Show file tree
Hide file tree
Showing 25 changed files with 45 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# next

- Convert all asUint...List to Uint...List.view

# 1.17.0

- Update **Argon2** interface:
Expand Down
6 changes: 3 additions & 3 deletions lib/src/algorithms/argon2/argon2_32bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class Argon2Internal {
int i, j, k, p;
int pass, slice, lane;
var hash0 = Uint8List(64 + 8);
var hash0as32 = hash0.buffer.asUint32List();
var hash0as32 = Uint32List.view(hash0.buffer);
var buffer32 = Uint32List(ctx.blocks << 8);
var buffer = buffer32.buffer.asUint8List();
var buffer = Uint8List.view(buffer32.buffer);
var result = Uint8List(ctx.hashLength);

// H_0 Generation (64 + 8 = 72 bytes)
Expand Down Expand Up @@ -79,7 +79,7 @@ class Argon2Internal {
// Finalization
/* XOR the blocks */
j = ctx.columns - 1;
var block = buffer.buffer.asUint8List(j << 10, 1024);
var block = Uint8List.view(buffer.buffer, j << 10, 1024);
for (k = 1; k < ctx.lanes; ++k) {
j += ctx.columns;
p = j << 10;
Expand Down
6 changes: 3 additions & 3 deletions lib/src/algorithms/argon2/argon2_64bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class Argon2Internal {
Uint8List convert(List<int> password) {
int i, j, k, cols;
int pass, slice, lane;
var hash0_32 = _hash0.buffer.asUint32List();
var memoryBytes = _memory.buffer.asUint8List();
var hash0_32 = Uint32List.view(_hash0.buffer);
var memoryBytes = Uint8List.view(_memory.buffer);

// H_0 Generation (64 + 8 = 72 bytes)
_initialHash(_hash0, password);
Expand Down Expand Up @@ -75,7 +75,7 @@ class Argon2Internal {

// Finalization : XOR the last column blocks
j = cols - 1024;
var block = memoryBytes.buffer.asUint8List(j, 1024);
var block = Uint8List.view(memoryBytes.buffer, j, 1024);
for (k = 1; k < ctx.lanes; ++k) {
j += cols;
for (i = 0; i < 1024; ++i) {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/algorithms/argon2/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import 'dart:typed_data';

import 'package:hashlib/hashlib.dart';
import 'package:hashlib/src/core/hash_digest.dart';
import 'package:hashlib/src/random.dart';
import 'package:hashlib_codecs/hashlib_codecs.dart';

const int _slices = 4;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/algorithms/blake2b_32bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,6 @@ class Blake2bHash extends BlockHashSink with MACSinkBase {
$update(buffer, 0, true);

// Convert the state to 8-bit byte array
return state.buffer.asUint8List().sublist(0, hashLength);
return Uint8List.view(state.buffer).sublist(0, hashLength);
}
}
4 changes: 2 additions & 2 deletions lib/src/algorithms/blake2b_64bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Blake2bHash extends BlockHashSink with MACSinkBase {
@override
final int hashLength;

late final Uint64List qbuffer = buffer.buffer.asUint64List();
late final Uint64List qbuffer = Uint64List.view(buffer.buffer);

/// For internal use only.
Blake2bHash(
Expand Down Expand Up @@ -309,6 +309,6 @@ class Blake2bHash extends BlockHashSink with MACSinkBase {
$update(buffer, 0, true);

// Convert the state to 8-bit byte array
return state.buffer.asUint8List().sublist(0, hashLength);
return Uint8List.view(state.buffer).sublist(0, hashLength);
}
}
2 changes: 1 addition & 1 deletion lib/src/algorithms/blake2s.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,6 @@ class Blake2sHash extends BlockHashSink with MACSinkBase {
$update(buffer, 0, true);

// Convert the state to 8-bit byte array
return state.buffer.asUint8List().sublist(0, hashLength);
return Uint8List.view(state.buffer).sublist(0, hashLength);
}
}
2 changes: 1 addition & 1 deletion lib/src/algorithms/keccak_64bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class KeccakHash extends BlockHashSink {
if (stateSize < 0 || stateSize > 100) {
throw ArgumentError('The state size is not valid');
}
qstate = buffer.buffer.asUint64List();
qstate = Uint64List.view(buffer.buffer);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/src/algorithms/md4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ class MD4Hash extends BlockHashSink {
$update();

// Convert the state to 8-bit byte array
return state.buffer.asUint8List().sublist(0, hashLength);
return Uint8List.view(state.buffer).sublist(0, hashLength);
}
}
2 changes: 1 addition & 1 deletion lib/src/algorithms/md5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,6 @@ class MD5Hash extends BlockHashSink {
$update();

// Convert the state to 8-bit byte array
return state.buffer.asUint8List().sublist(0, hashLength);
return Uint8List.view(state.buffer).sublist(0, hashLength);
}
}
5 changes: 3 additions & 2 deletions lib/src/algorithms/poly1305/poly1305_32bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ class Poly1305Sink extends BlockHashSink with MACSinkBase {

_h += _s;

return Uint32List.fromList([
var result = Uint32List.fromList([
(_h % _m).toUnsigned(32).toInt(),
((_h >> 32) % _m).toUnsigned(32).toInt(),
((_h >> 64) % _m).toUnsigned(32).toInt(),
((_h >> 96) % _m).toUnsigned(32).toInt(),
]).buffer.asUint8List();
]);
return Uint8List.view(result.buffer);
}
}
5 changes: 3 additions & 2 deletions lib/src/algorithms/poly1305/poly1305_64bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,12 @@ class Poly1305Sink extends BlockHashSink with MACSinkBase {
_h2 += _s2 + (_h1 >>> 32);
_h3 += _s3 + (_h2 >>> 32);

return Uint32List.fromList([
var result = Uint32List.fromList([
_h0,
_h1,
_h2,
_h3,
]).buffer.asUint8List();
]);
return Uint8List.view(result.buffer);
}
}
2 changes: 1 addition & 1 deletion lib/src/algorithms/ripemd128.dart
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,6 @@ class RIPEMD128Hash extends BlockHashSink {
$update();

// Convert the state to 8-bit byte array
return state.buffer.asUint8List().sublist(0, hashLength);
return Uint8List.view(state.buffer).sublist(0, hashLength);
}
}
2 changes: 1 addition & 1 deletion lib/src/algorithms/ripemd160.dart
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,6 @@ class RIPEMD160Hash extends BlockHashSink {
$update();

// Convert the state to 8-bit byte array
return state.buffer.asUint8List().sublist(0, hashLength);
return Uint8List.view(state.buffer).sublist(0, hashLength);
}
}
2 changes: 1 addition & 1 deletion lib/src/algorithms/ripemd256.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,6 @@ class RIPEMD256Hash extends BlockHashSink {
$update();

// Convert the state to 8-bit byte array
return state.buffer.asUint8List().sublist(0, hashLength);
return Uint8List.view(state.buffer).sublist(0, hashLength);
}
}
2 changes: 1 addition & 1 deletion lib/src/algorithms/ripemd320.dart
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,6 @@ class RIPEMD320Hash extends BlockHashSink {
$update();

// Convert the state to 8-bit byte array
return state.buffer.asUint8List().sublist(0, hashLength);
return Uint8List.view(state.buffer).sublist(0, hashLength);
}
}
2 changes: 1 addition & 1 deletion lib/src/algorithms/scrypt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Scrypt extends KeyDerivatorBase {
// Derive the inner blocks
var sink = HMACSink(sha256.createSink());
var inner = PBKDF2(sink, salt, 1, innerKeyLength).convert(password);
Uint32List inner32 = inner.buffer.asUint32List();
var inner32 = Uint32List.view(inner.buffer);

/// [length] = 128 * r = 2 * 64 * r = 4 * 32 * r bytes
@pragma('vm:prefer-inline')
Expand Down
2 changes: 1 addition & 1 deletion lib/src/algorithms/sm3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,6 @@ class SM3Hash extends BlockHashSink {
for (int i = 0; i < output.length; i++) {
output[i] = _swap32(state[i]);
}
return output.buffer.asUint8List();
return Uint8List.view(output.buffer);
}
}
8 changes: 4 additions & 4 deletions lib/src/algorithms/xxh3_128_64bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class XXH3Sink128bit extends BlockHashSink {
final Uint8List secret;
final Uint64List state = Uint64List(8);
final ListQueue<int> last = ListQueue<int>(_midsizeMax);
late final Uint64List qbuffer = buffer.buffer.asUint64List();
late final Uint64List qbuffer = Uint64List.view(buffer.buffer);
late final Uint64List secret64 = Uint64List.view(secret.buffer);
late final ByteData secretBD = secret.buffer.asByteData();
late final Uint64List secret64 = secret.buffer.asUint64List();

@override
final int hashLength = 16;
Expand All @@ -61,7 +61,7 @@ class XXH3Sink128bit extends BlockHashSink {
return XXH3Sink128bit.withSecret();
}
var secret = Uint8List.fromList(_kSecret);
var secret64 = secret.buffer.asUint64List();
var secret64 = Uint64List.view(secret.buffer);
for (int i = 0; i < secret64.length; i += 2) {
secret64[i] += seed;
}
Expand Down Expand Up @@ -486,7 +486,7 @@ class XXH3Sink128bit extends BlockHashSink {
int i;
ByteData key;
Uint64List input = Uint64List(_midsizeMax >>> 3);
Uint8List input8 = input.buffer.asUint8List();
Uint8List input8 = Uint8List.view(input.buffer);

if (messageLength <= _midsizeMax) {
var it = last.iterator;
Expand Down
8 changes: 4 additions & 4 deletions lib/src/algorithms/xxh3_64_64bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class XXH3Sink64bit extends BlockHashSink {
final Uint8List secret;
final Uint64List acc = Uint64List(8);
final ListQueue<int> last = ListQueue<int>(_midsizeMax);
late final Uint64List qbuffer = buffer.buffer.asUint64List();
late final Uint64List qbuffer = Uint64List.view(buffer.buffer);
late final Uint64List secret64 = Uint64List.view(secret.buffer);
late final ByteData secretBD = secret.buffer.asByteData();
late final Uint64List secret64 = secret.buffer.asUint64List();

@override
final int hashLength = 8;
Expand All @@ -61,7 +61,7 @@ class XXH3Sink64bit extends BlockHashSink {
return XXH3Sink64bit.withSecret();
}
var secret = Uint8List.fromList(_kSecret);
var secret64 = secret.buffer.asUint64List();
var secret64 = Uint64List.view(secret.buffer);
for (int i = 0; i < secret64.length; i += 2) {
secret64[i] += seed;
}
Expand Down Expand Up @@ -357,7 +357,7 @@ class XXH3Sink64bit extends BlockHashSink {
int hash;
ByteData key;
Uint64List input = Uint64List(_midsizeMax >>> 3);
Uint8List input8 = input.buffer.asUint8List();
Uint8List input8 = Uint8List.view(input.buffer);

if (messageLength <= _midsizeMax) {
var it = last.iterator;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/algorithms/xxh64_64bit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class XXHash64Sink extends BlockHashSink {
int _acc3 = 0;
int _acc4 = 0;

late final Uint64List qbuffer = buffer.buffer.asUint64List();
late final Uint64List qbuffer = Uint64List.view(buffer.buffer);

XXHash64Sink(this.seed) : super(32) {
reset();
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/block_hash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class BlockHashSink implements HashDigestSink {
/// - [bufferLength] is the buffer length where blocks are stored temporarily
BlockHashSink(this.blockLength, {int? bufferLength}) : super() {
buffer = Uint8List(bufferLength ?? blockLength);
sbuffer = buffer.buffer.asUint32List();
sbuffer = Uint32List.view(buffer.buffer);
bdata = buffer.buffer.asByteData();
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/hash_digest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class HashDigest extends Object {
if (other is HashDigest) {
return isEqual(other.bytes);
} else if (other is ByteBuffer) {
return isEqual(buffer.asUint8List());
return isEqual(Uint8List.view(buffer));
} else if (other is TypedData && other is! Uint8List) {
return isEqual(other.buffer.asUint8List());
return isEqual(Uint8List.view(other.buffer));
} else if (other is String) {
return isEqual(fromHex(other));
} else if (other is Iterable<int>) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/random.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void fillRandom(
length = min(length + start, buffer.lengthInBytes);
}
random ??= _defaultGenerator();
var data = buffer.asUint8List();
var data = Uint8List.view(buffer);
for (int i = start; i < length; i++) {
data[i] = random.nextInt(256);
}
Expand Down
2 changes: 1 addition & 1 deletion test/random_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() {
int sum = 0;
for (int i = 0; i < 100; ++i) {
fillRandom(list.buffer, start: 5, length: 9);
var bytes = list.buffer.asUint8List();
var bytes = Uint8List.view(list.buffer);
sum += bytes.skip(5).take(9).any((n) => n > 0) ? 1 : 0;
expect(bytes.take(5).toList(), equals([0, 0, 0, 0, 0]));
expect(bytes.skip(5 + 9).toList(), equals([0, 0, 0, 0, 0, 0]));
Expand Down

0 comments on commit 1129d26

Please sign in to comment.