boardgames_app/lib/utils/crypto.dart
2025-06-14 14:28:16 +02:00

35 lines
1.0 KiB
Dart

import 'dart:math';
import 'dart:typed_data';
import "package:pointycastle/export.dart";
AsymmetricKeyPair<RSAPublicKey, RSAPrivateKey> generateRSAkeyPair(
SecureRandom secureRandom,
{int bitLength = 2048}) {
// Create an RSA key generator and initialize it
// final keyGen = KeyGenerator('RSA'); // Get using registry
final keyGen = RSAKeyGenerator();
keyGen.init(ParametersWithRandom(
RSAKeyGeneratorParameters(BigInt.parse('65537'), bitLength, 64),
secureRandom));
// Use the generator
final pair = keyGen.generateKeyPair();
// Cast the generated key pair into the RSA key types
final myPublic = pair.publicKey as RSAPublicKey;
final myPrivate = pair.privateKey as RSAPrivateKey;
return AsymmetricKeyPair<RSAPublicKey, RSAPrivateKey>(myPublic, myPrivate);
}
SecureRandom getSecureRandom() {
final rnd = Random.secure();
final secureRandom = SecureRandom('Fortuna')
..seed(KeyParameter(
Uint8List.fromList(List.generate(32, (index) => rnd.nextInt(256)))));
return secureRandom;
}