Skip to content

Replace KeyPair class with record typedef KeyPair<T,S> #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.ec_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ KeyPair<_EvpPKey, _EvpPKey> _generateEcKeyPair(
final pubKey = _EvpPKey();
_checkOpIsOne(ssl.EVP_PKEY_set1_EC_KEY.invoke(pubKey, ecPub));

return createKeyPair(
privKey,
pubKey,
return (
privateKey: privKey,
publicKey: pubKey,
);
});
}
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>>
EllipticCurve curve,
) async {
final p = _generateEcKeyPair(curve);
return createKeyPair(
_EcdhPrivateKeyImpl(p.privateKey),
_EcdhPublicKeyImpl(p.publicKey),
return (
privateKey: _EcdhPrivateKeyImpl(p.privateKey),
publicKey: _EcdhPublicKeyImpl(p.publicKey),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.ecdsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ Future<KeyPair<EcdsaPrivateKeyImpl, EcdsaPublicKeyImpl>>
EllipticCurve curve,
) async {
final p = _generateEcKeyPair(curve);
return createKeyPair(
_EcdsaPrivateKeyImpl(p.privateKey),
_EcdsaPublicKeyImpl(p.publicKey),
return (
privateKey: _EcdsaPrivateKeyImpl(p.privateKey),
publicKey: _EcdsaPublicKeyImpl(p.publicKey),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.rsa_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ KeyPair<_EvpPKey, _EvpPKey> _generateRsaKeyPair(
final pubKey = _EvpPKey();
_checkOp(ssl.EVP_PKEY_set1_RSA.invoke(pubKey, pubRSA) == 1);

return createKeyPair(
privKey,
pubKey,
return (
privateKey: privKey,
publicKey: pubKey,
);
});
}
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>>
// Get hash first, to avoid a leak of EVP_PKEY if _HashImpl.fromHash throws
final h = _HashImpl.fromHash(hash);
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
return createKeyPair(
_RsaOaepPrivateKeyImpl(keys.privateKey, h),
_RsaOaepPublicKeyImpl(keys.publicKey, h),
return (
privateKey: _RsaOaepPrivateKeyImpl(keys.privateKey, h),
publicKey: _RsaOaepPublicKeyImpl(keys.publicKey, h),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.rsapss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ Future<KeyPair<RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl>>
// Validate and get hash function
final h = _HashImpl.fromHash(hash);
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
return createKeyPair(
_RsaPssPrivateKeyImpl(keys.privateKey, h),
_RsaPssPublicKeyImpl(keys.publicKey, h),
return (
privateKey: _RsaPssPrivateKeyImpl(keys.privateKey, h),
publicKey: _RsaPssPublicKeyImpl(keys.publicKey, h),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ Future<KeyPair<RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl>>
// Get hash first, to avoid a leak of EVP_PKEY if _HashImpl.fromHash throws
final h = _HashImpl.fromHash(hash);
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
return createKeyPair(
_RsaSsaPkcs1V15PrivateKeyImpl(keys.privateKey, h),
_RsaSsaPkcs1V15PublicKeyImpl(keys.publicKey, h),
return (
privateKey: _RsaSsaPkcs1V15PrivateKeyImpl(keys.privateKey, h),
publicKey: _RsaSsaPkcs1V15PublicKeyImpl(keys.publicKey, h),
);
}

Expand Down
15 changes: 1 addition & 14 deletions lib/src/impl_interface/impl_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,7 @@ part 'impl_interface.digest.dart';
part 'impl_interface.random.dart';

/// A key-pair as returned from key generation.
class KeyPair<S, T> {
KeyPair._(this.privateKey, this.publicKey); // keep the constructor private.

/// Private key for [publicKey].
final S privateKey;

/// Public key matching [privateKey].
final T publicKey;
}

/// Factory method to create KeyPair instance
KeyPair<S, T> createKeyPair<S, T>(S privateKey, T publicKey) {
return KeyPair._(privateKey, publicKey);
}
typedef KeyPair<T, S> = ({T privateKey, S publicKey});

/// Elliptic curves supported by ECDSA and ECDH.
///
Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_js/impl_js.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>>
),
_usagesDeriveBits,
);
return createKeyPair(
_EcdhPrivateKeyImpl(pair.privateKey),
_EcdhPublicKeyImpl(pair.publicKey),
return (
privateKey: _EcdhPrivateKeyImpl(pair.privateKey),
publicKey: _EcdhPublicKeyImpl(pair.publicKey),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_js/impl_js.ecdsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ Future<KeyPair<EcdsaPrivateKeyImpl, EcdsaPublicKeyImpl>>
),
_usagesSignVerify,
);
return createKeyPair(
_EcdsaPrivateKeyImpl(pair.privateKey),
_EcdsaPublicKeyImpl(pair.publicKey),
return (
privateKey: _EcdsaPrivateKeyImpl(pair.privateKey),
publicKey: _EcdsaPublicKeyImpl(pair.publicKey),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_js/impl_js.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>>
),
_usagesEncryptDecrypt,
);
return createKeyPair(
_RsaOaepPrivateKeyImpl(pair.privateKey),
_RsaOaepPublicKeyImpl(pair.publicKey),
return (
privateKey: _RsaOaepPrivateKeyImpl(pair.privateKey),
publicKey: _RsaOaepPublicKeyImpl(pair.publicKey),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_js/impl_js.rsapss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ Future<KeyPair<RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl>>
),
_usagesSignVerify,
);
return createKeyPair(
_RsaPssPrivateKeyImpl(pair.privateKey),
_RsaPssPublicKeyImpl(pair.publicKey),
return (
privateKey: _RsaPssPrivateKeyImpl(pair.privateKey),
publicKey: _RsaPssPublicKeyImpl(pair.publicKey),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_js/impl_js.rsassapkcs1v15.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Future<KeyPair<RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl>>
),
_usagesSignVerify,
);
return createKeyPair(
_RsaSsaPkcs1V15PrivateKeyImpl(pair.privateKey),
_RsaSsaPkcs1V15PublicKeyImpl(pair.publicKey),
return (
privateKey: _RsaSsaPkcs1V15PrivateKeyImpl(pair.privateKey),
publicKey: _RsaSsaPkcs1V15PublicKeyImpl(pair.publicKey),
);
}

Expand Down
47 changes: 21 additions & 26 deletions lib/src/testing/utils/testrunner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,6 @@ typedef DeriveBitsFn<T> = Future<List<int>> Function(
Map<String, dynamic> deriveParams,
);

class _KeyPair<S, T> implements KeyPair<S, T> {
@override
final S privateKey;

@override
final T publicKey;

_KeyPair({required this.privateKey, required this.publicKey});
}

@sealed
class TestRunner<PrivateKey, PublicKey> {
final String algorithm;
Expand Down Expand Up @@ -402,7 +392,7 @@ class TestRunner<PrivateKey, PublicKey> {
exportPrivateJsonWebKey: exportPrivateJsonWebKey,
generateKeyPair: (params) async {
final k = await generateKey(params);
return _KeyPair(privateKey: k, publicKey: k);
return (privateKey: k, publicKey: k);
},
signBytes: signBytes,
signStream: signStream,
Expand Down Expand Up @@ -867,9 +857,10 @@ void _runTests<PrivateKey, PublicKey>(
} else {
test('create derivedBits', () async {
derivedBits = await r._deriveBits(
_KeyPair(
privateKey: privateKey as PrivateKey,
publicKey: publicKey as PublicKey),
(
privateKey: privateKey as PrivateKey,
publicKey: publicKey as PublicKey
),
c.derivedLength!,
c.deriveParams!,
);
Expand All @@ -878,9 +869,10 @@ void _runTests<PrivateKey, PublicKey>(

test('validated derivedBits', () async {
final derived = await r._deriveBits(
_KeyPair(
privateKey: privateKey as PrivateKey,
publicKey: publicKey as PublicKey),
(
privateKey: privateKey as PrivateKey,
publicKey: publicKey as PublicKey
),
c.derivedLength!,
c.deriveParams!,
);
Expand Down Expand Up @@ -983,9 +975,10 @@ void _runTests<PrivateKey, PublicKey>(
}
if (r._deriveBits != null) {
final derived = await r._deriveBits(
_KeyPair(
privateKey: privateKey as PrivateKey,
publicKey: publicKey as PublicKey),
(
privateKey: privateKey as PrivateKey,
publicKey: publicKey as PublicKey
),
c.derivedLength!,
c.deriveParams!,
);
Expand All @@ -1009,9 +1002,10 @@ void _runTests<PrivateKey, PublicKey>(
}
if (r._deriveBits != null) {
final derived = await r._deriveBits(
_KeyPair(
privateKey: privateKey as PrivateKey,
publicKey: publicKey as PublicKey),
(
privateKey: privateKey as PrivateKey,
publicKey: publicKey as PublicKey
),
c.derivedLength!,
c.deriveParams!,
);
Expand Down Expand Up @@ -1363,9 +1357,10 @@ void _runTests<PrivateKey, PublicKey>(
if (r._deriveBits != null) {
test('deriveBits', () async {
final derived = await r._deriveBits(
_KeyPair(
privateKey: privateKey as PrivateKey,
publicKey: publicKey as PublicKey),
(
privateKey: privateKey as PrivateKey,
publicKey: publicKey as PublicKey
),
c.derivedLength!,
c.deriveParams!,
);
Expand Down
13 changes: 1 addition & 12 deletions lib/src/testing/webcrypto/ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ import '../utils/utils.dart';
import '../utils/testrunner.dart';
import '../utils/detected_runtime.dart';

class _KeyPair<S extends EcdhPrivateKey, T extends EcdhPublicKey>
implements KeyPair<S, T> {
@override
final S privateKey;

@override
final T publicKey;

_KeyPair({required this.privateKey, required this.publicKey});
}

final runner = TestRunner.asymmetric<EcdhPrivateKey, EcdhPublicKey>(
algorithm: 'ECDH',
importPrivateRawKey: null, // not supported
Expand Down Expand Up @@ -58,7 +47,7 @@ final runner = TestRunner.asymmetric<EcdhPrivateKey, EcdhPublicKey>(
final b = await EcdhPrivateKey.generateKey(curveFromJson(
generateKeyPairParams,
));
return _KeyPair(
return (
privateKey: a.privateKey,
publicKey: b.publicKey,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/webcrypto/webcrypto.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ final class EcdhPrivateKey {
final privateKey = EcdhPrivateKey(privateKeyImpl);
final publicKey = EcdhPublicKey(publicKeyImpl);

return createKeyPair(privateKey, publicKey);
return (privateKey: privateKey, publicKey: publicKey);
}

/// Derive a shared secret from two ECDH key pairs using the private key from one pair
Expand Down
2 changes: 1 addition & 1 deletion lib/src/webcrypto/webcrypto.ecdsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class EcdsaPrivateKey {
final privateKey = EcdsaPrivateKey(privateKeyImpl);
final publicKey = EcdsaPublicKey(publicKeyImpl);

return createKeyPair(privateKey, publicKey);
return (privateKey: privateKey, publicKey: publicKey);
}

/// TODO: Document that this returns the raw signature format specified
Expand Down
2 changes: 1 addition & 1 deletion lib/src/webcrypto/webcrypto.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ final class RsaOaepPrivateKey {
final privateKey = RsaOaepPrivateKey(privateKeyImpl);
final publicKey = RsaOaepPublicKey(publicKeyImpl);

return createKeyPair(privateKey, publicKey);
return (privateKey: privateKey, publicKey: publicKey);
}

/// Decrypt [data] encrypted with [RsaOaepPublicKey.encryptBytes] from the
Expand Down
2 changes: 1 addition & 1 deletion lib/src/webcrypto/webcrypto.rsapss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ final class RsaPssPrivateKey {
final privateKey = RsaPssPrivateKey(privateKeyImpl);
final publicKey = RsaPssPublicKey(publicKeyImpl);

return createKeyPair(privateKey, publicKey);
return (privateKey: privateKey, publicKey: publicKey);
}

/// Sign [data] with this RSASSA-PSS private key.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ final class RsassaPkcs1V15PrivateKey {
final privateKey = RsassaPkcs1V15PrivateKey(privateKeyImpl);
final publicKey = RsassaPkcs1V15PublicKey(publicKeyImpl);

return createKeyPair(privateKey, publicKey);
return (privateKey: privateKey, publicKey: publicKey);
}

/// Sign [data] with this RSASSA-PKCS1-v1_5 private key.
Expand Down
Loading