From 7856777c35d13687ad3fb30a64d2f5bf123feadc Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Wed, 4 Dec 2024 23:20:34 +0900 Subject: [PATCH] refactor: Added processing branching by kIsWasm --- lib/src/crypto_subtle.dart | 36 +++++++++++++++++++++++------------- test/crypto_subtle_test.dart | 19 ++++++++++++++++++- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/lib/src/crypto_subtle.dart b/lib/src/crypto_subtle.dart index 65c36a4b..81b724c0 100644 --- a/lib/src/crypto_subtle.dart +++ b/lib/src/crypto_subtle.dart @@ -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; @@ -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 decrypt( diff --git a/test/crypto_subtle_test.dart b/test/crypto_subtle_test.dart index 605a710f..2170c4ab 100644 --- a/test/crypto_subtle_test.dart +++ b/test/crypto_subtle_test.dart @@ -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( @@ -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 {