Skip to content

Commit

Permalink
refactor: Added processing branching by kIsWasm
Browse files Browse the repository at this point in the history
  • Loading branch information
koji-1009 committed Dec 4, 2024
1 parent cbb5206 commit 27c1bb3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
77 changes: 44 additions & 33 deletions lib/src/crypto_subtle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ library common;
import 'dart:js_interop';
import 'dart:typed_data';

import 'package:meta/meta.dart';
import 'package:flutter/foundation.dart';

import 'jsonwebkey.dart' show JsonWebKey, RsaOtherPrimesInfo;

Expand Down Expand Up @@ -333,39 +333,50 @@ extension type JSRsaOtherPrimesInfo(JSObject _) implements JSObject {
}

TypedData getRandomValues(TypedData array) {
if (array is Uint8List) {
final values = array.toJS;
window.crypto.getRandomValues(values);
array.setAll(0, values.toDart);
return values.toDart;
} else if (array is Uint16List) {
final values = array.toJS;
window.crypto.getRandomValues(values);
array.setAll(0, values.toDart);
return values.toDart;
} else if (array is Uint32List) {
final values = array.toJS;
window.crypto.getRandomValues(values);
array.setAll(0, values.toDart);
return values.toDart;
} else if (array is Int8List) {
final values = array.toJS;
window.crypto.getRandomValues(values);
array.setAll(0, values.toDart);
return values.toDart;
} else if (array is Int16List) {
final values = array.toJS;
window.crypto.getRandomValues(values);
array.setAll(0, values.toDart);
return values.toDart;
} else if (array is Int32List) {
final values = array.toJS;
window.crypto.getRandomValues(values);
array.setAll(0, values.toDart);
return values.toDart;
} else {
throw UnsupportedError('Unsupported TypedData type ${array.runtimeType}');
// Since dart2wasm does not reflect values in the array, use setAll to reflect them.
// see: https://github.com/dart-lang/sdk/issues/59651
switch (array) {
case Uint8List():
final values = array.toJS;
window.crypto.getRandomValues(values);
if (kIsWasm) {
array.setAll(0, values.toDart);
}
case Uint16List():
final values = array.toJS;
window.crypto.getRandomValues(values);
if (kIsWasm) {
array.setAll(0, values.toDart);
}
case Uint32List():
final values = array.toJS;
window.crypto.getRandomValues(values);
if (kIsWasm) {
array.setAll(0, values.toDart);
}
case Int8List():
final values = array.toJS;
window.crypto.getRandomValues(values);
if (kIsWasm) {
array.setAll(0, values.toDart);
}
case Int16List():
final values = array.toJS;
window.crypto.getRandomValues(values);
if (kIsWasm) {
array.setAll(0, values.toDart);
}
case Int32List():
final values = array.toJS;
window.crypto.getRandomValues(values);
if (kIsWasm) {
array.setAll(0, values.toDart);
}
default:
throw UnsupportedError('Unsupported TypedData type ${array.runtimeType}');
}

return array;
}

Future<ByteBuffer> decrypt(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/impl_js/impl_js.random.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ void fillRandomBytes(TypedData destination) {
rethrow;
}

throw _translateJavaScriptException(e);
throw _translateJavaScriptException();
}
}
19 changes: 18 additions & 1 deletion test/crypto_subtle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ void main() {
});

group('crypto', () {
test('getRandomValues: success', () {
final data = Uint8List(16 * 1024);
expect(
data.every((e) => e == 0),
isTrue,
);
subtle.window.crypto.getRandomValues(data.toJS);
expect(
data.any((e) => e != 0),
isTrue,
);
}, skip: 'dart2wasm');

test('getRandomValues: success', () {
final data = Uint8List(16 * 1024);
expect(
Expand All @@ -70,11 +83,15 @@ void main() {
);
final values = data.toJS;
subtle.window.crypto.getRandomValues(values);
expect(
data.every((e) => e == 0),
isTrue,
);
expect(
values.toDart.any((e) => e != 0),
isTrue,
);
});
}, skip: 'dart2js');

test('getRandomValues: too long', () {
try {
Expand Down

0 comments on commit 27c1bb3

Please sign in to comment.