newKeyPairFromSeed method
Generates a key pair from the seed.
This will throw UnsupportedError if the algorithm does not support seeds for private key generation.
Example
In this example, we use X25519
:
import 'dart:convert';
import 'package:cryptography/cryptography.dart';
Future<void> main() async {
// X25519 seed is any 32 bytes.
// We can use SHA256, which computes a 32-byte hash from any input.
final seed = utf8.encode('example');
final seedWith32Bytes = (await Sha256().hash(seed)).bytes;
final algorithm = X25519();
final keyPair = await algorithm.newKeyPairFromSeed(seedWith32Bytes);
}
Implementation
@override
Future<EcKeyPair> newKeyPairFromSeed(List<int> seed) async {
if (isSupportedPlatform) {
final result = await invokeMethod(
'Ecdh.newKeyPair',
{
if (isAndroid) 'androidProvider': androidCryptoProvider,
'curve': _curveName,
'seed': asUint8List(seed),
},
);
final der = result['der'] as Uint8List?;
if (der != null) {
return EcKeyPairData.parseDer(
der,
type: keyPairType,
);
}
final d = result['d'] as Uint8List;
final x = result['x'] as Uint8List;
final y = result['y'] as Uint8List;
return EcKeyPairData(
d: d,
x: x,
y: y,
type: keyPairType,
);
}
final fallback = this.fallback;
if (fallback == null) {
throw UnsupportedError('Unsupported and no fallback implementation');
}
return await fallback.newKeyPair();
}