dev
dart 22 lines 708 Bytes
Raw
1 import 'dart:io';
2 import 'dart:math';
3 import 'dart:typed_data';
4
5 import 'package:flutter/services.dart';
6
7 const utils = const MethodChannel('com.cake_wallet/native_utils');
8
9 Future<Uint8List> secRandom(int count) async {
10 try {
11 if (Platform.isWindows || Platform.isLinux) {
12 // Used method to get securely generated random bytes from cake backups
13 const byteSize = 256;
14 final rng = Random.secure();
15 return Uint8List.fromList(List<int>.generate(count, (_) => rng.nextInt(byteSize)));
16 }
17 return await utils.invokeMethod<Uint8List>('sec_random', {'count': count}) ??
18 Uint8List.fromList([]);
19 } on PlatformException catch (_) {
20 return Uint8List.fromList([]);
21 }
22 }