mirrored from https://www.bouncycastle.org/repositories/pc-dart
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathec_key_generator.dart
49 lines (37 loc) · 1.33 KB
/
ec_key_generator.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// See file LICENSE for more information.
library impl.key_generator.ec_key_generator;
import 'package:pointycastle/api.dart';
import 'package:pointycastle/ecc/api.dart';
import 'package:pointycastle/key_generators/api.dart';
import 'package:pointycastle/src/registry/registry.dart';
/// Abstract [CipherParameters] to init an ECC key generator.
class ECKeyGenerator implements KeyGenerator {
static final FactoryConfig factoryConfig = StaticFactoryConfig(KeyGenerator, 'EC', () => ECKeyGenerator());
late ECDomainParameters _params;
late SecureRandom _random;
@override
String get algorithmName => 'EC';
@override
void init(CipherParameters params) {
ECKeyGeneratorParameters ecparams;
if (params is ParametersWithRandom) {
_random = params.random;
ecparams = params.parameters as ECKeyGeneratorParameters;
} else {
_random = SecureRandom();
ecparams = params as ECKeyGeneratorParameters;
}
_params = ecparams.domainParameters;
}
@override
AsymmetricKeyPair<ECPublicKey, ECPrivateKey> generateKeyPair() {
var n = _params.n;
var nBitLength = n.bitLength;
BigInt? d;
do {
d = _random.nextBigInteger(nBitLength);
} while (d == BigInt.zero || (d >= n));
var Q = _params.G * d;
return AsymmetricKeyPair(ECPublicKey(Q, _params), ECPrivateKey(d, _params));
}
}