Add ability to get random bytes on Linux. Used same method as in cake backups.

This commit is contained in:
M 2023-04-21 18:02:19 -04:00
parent 5c2490e721
commit d45df36238

View file

@ -1,11 +1,18 @@
import 'dart:typed_data';
import 'dart:io';
import 'dart:math';
import 'package:flutter/services.dart';
const utils = const MethodChannel('com.cake_wallet/native_utils');
Future<Uint8List> secRandom(int count) async {
try {
if (Platform.isLinux) {
// Used method to get securely generated random bytes from cake backups
const byteSize = 256;
final rng = Random.secure();
return Uint8List.fromList(List<int>.generate(count, (_) => rng.nextInt(byteSize)));
}
return await utils.invokeMethod<Uint8List>('sec_random', {'count': count}) ?? Uint8List.fromList([]);
} on PlatformException catch (_) {
return Uint8List.fromList([]);