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 4e8e9de commit 7856777
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
36 changes: 23 additions & 13 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,49 @@ extension type JSRsaOtherPrimesInfo(JSObject _) implements JSObject {
}

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

return array;
}

Future<ByteBuffer> decrypt(
Expand Down
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 7856777

Please sign in to comment.