|
1 | | -# rsa_key_generator |
| 1 | +# Flutter RSA Key Generator |
2 | 2 |
|
3 | | -Flutter app to generate RSA keys |
| 3 | +This project shows how to use [Pointy Castle](https://github.com/PointyCastle/pointycastle) to generate a RSA Key and encode it to a PKCS1 Pem String. |
4 | 4 |
|
5 | | -## Getting Started |
| 5 | + |
6 | 6 |
|
7 | | -This project is a starting point for a Flutter application. |
| 7 | +In order to generate a new `RSA Keypair` we use `RSAKeyGenerator` |
8 | 8 |
|
9 | | -A few resources to get you started if this is your first Flutter project: |
| 9 | +``` |
| 10 | +AsymmetricKeyPair<PublicKey, PrivateKey> computeRSAKeyPair( |
| 11 | + SecureRandom secureRandom) { |
| 12 | + var rsapars = new RSAKeyGeneratorParameters(BigInt.from(65537), 2048, 12); |
| 13 | + var params = new ParametersWithRandom(rsapars, secureRandom); |
| 14 | + var keyGenerator = new RSAKeyGenerator(); |
| 15 | + keyGenerator.init(params); |
| 16 | + return keyGenerator.generateKeyPair(); |
| 17 | +} |
| 18 | +``` |
10 | 19 |
|
11 | | -- [Lab: Write your first Flutter app](https://flutter.io/docs/get-started/codelab) |
12 | | -- [Cookbook: Useful Flutter samples](https://flutter.io/docs/cookbook) |
| 20 | +To be able generate the keys in a background thread we use Dart's [Isolate](https://api.dartlang.org/stable/2.1.0/dart-isolate/dart-isolate-library.html) implemented in Flutter's [compute](https://docs.flutter.io/flutter/foundation/compute.html). We must ensure that the `computeRSAKeyPair` function is placed __outside__ a `Class` so that it can be called globally. |
13 | 21 |
|
14 | | -For help getting started with Flutter, view our |
15 | | -[online documentation](https://flutter.io/docs), which offers tutorials, |
16 | | -samples, guidance on mobile development, and a full API reference. |
| 22 | +``` |
| 23 | +Future<AsymmetricKeyPair<PublicKey, PrivateKey>> getRSAKeyPair( |
| 24 | + SecureRandom secureRandom) async { |
| 25 | + return await compute(computeRSAKeyPair, secureRandom); |
| 26 | +} |
| 27 | +``` |
0 commit comments