Skip to content

Commit

Permalink
Merge pull request #36 from konstantinullrich/feature/#35_Make_RSAKey…
Browse files Browse the repository at this point in the history
…pair.fromRandom()_async

Add Keypair Factory
  • Loading branch information
konstantinullrich authored Oct 28, 2022
2 parents 3b34220 + 23f96b8 commit e014ad8
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.0
- Add `KeypairFactory`, `RSAKeypairFactory` and `ECKeypairFactory`
- Update dependencies

## 2.0.5
- Update Example Doc
- Minor Bugfixes
Expand Down
5 changes: 5 additions & 0 deletions lib/crypton.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ library crypton;
export 'src/private_key.dart';
export 'src/public_key.dart';
export 'src/keypair.dart';
export 'src/keypair_factory.dart';

/// Rivest-Shamir-Adleman Cryptography (RSA)
///
/// [RSAPrivateKey] can be used to sign and decrypt data using the RSA Algorithm
/// [RSAPublicKey] can be used to verify signatures and encrypt data using RSA
/// [RSAKeypair] is used to generate a pair of a [RSAPrivateKey] and a [RSAPublicKey]
/// [RSAKeypairFactory] is used to generate a [RSAKeypair]
export 'src/rsa/private_key.dart';
export 'src/rsa/public_key.dart';
export 'src/rsa/keypair.dart';
export 'src/rsa/keypair_factory.dart';

/// Elliptic Curve Cryptography (ECC)
///
/// [ECPrivateKey] can be used to sign [String]s using ECC
/// [ECPublicKey] can be used to verify signatures using ECC
/// [ECKeypair] is used to generate a pair of a [ECPrivateKey] and a [ECPublicKey]
/// [ECKeypairFactory] is used to generate a [ECKeypair]
/// [ECPoint] is a Point on the elliptic Curve
export 'src/ec/private_key.dart';
export 'src/ec/public_key.dart';
export 'src/ec/keypair.dart';
export 'src/ec/keypair_factory.dart';
export 'src/ec/ecpoint.dart';
16 changes: 16 additions & 0 deletions lib/src/ec/keypair_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:crypton/crypton.dart';

/// [KeypairFactory] using the EC Algorithm
class ECKeypairFactory implements KeypairFactory {
/// Create a fresh [ECKeypairFactory]
ECKeypairFactory();

/// Generate a random [ECKeypair]
@override
ECKeypair fromRandom() => ECKeypair.fromRandom();

/// Generate a random [ECKeypair] asynchronously
@override
Future<ECKeypair> fromRandomAsync() async =>
Future(() => ECKeypair.fromRandom());
}
12 changes: 12 additions & 0 deletions lib/src/keypair_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:crypton/crypton.dart';

abstract class KeypairFactory {
/// Create a fresh [KeypairFactory]
KeypairFactory();

/// Generate a random [Keypair]
Keypair fromRandom();

/// Generate a random [Keypair] asynchronously
Future<Keypair> fromRandomAsync();
}
17 changes: 17 additions & 0 deletions lib/src/rsa/keypair_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:crypton/crypton.dart';

/// [KeypairFactory] using the RSA Algorithm
class RSAKeypairFactory implements KeypairFactory {
/// Create a fresh [RSAKeypairFactory]
RSAKeypairFactory();

/// Generate a random [RSAKeypair]
@override
RSAKeypair fromRandom({int keySize = 2048}) =>
RSAKeypair.fromRandom(keySize: keySize);

/// Generate a random [RSAKeypair] asynchronously
@override
Future<RSAKeypair> fromRandomAsync({int keySize = 2048}) async =>
Future(() => RSAKeypair.fromRandom(keySize: keySize));
}
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: crypton
description: A simple Dart library for asymmetric encryption and digital signatures
version: 2.0.5
version: 2.1.0
homepage: https://github.com/konstantinullrich
repository: https://github.com/konstantinullrich/crypton
issue_tracker: https://github.com/konstantinullrich/crypton/issues
Expand All @@ -9,9 +9,9 @@ environment:
sdk: '>=2.14.0 <3.0.0'

dependencies:
pointycastle: ^3.4.0
asn1lib: ^1.0.3
pointycastle: ^3.6.2
asn1lib: ^1.2.2

dev_dependencies:
lints: ^1.0.1
test: ^1.19.3
test: ^1.21.4
45 changes: 36 additions & 9 deletions test/crypton_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,32 @@ void main() {
});
});

group('KeyPair Factory Tests', () {
test('Generate a random RSA Keypair', () {
final keypair = RSAKeypairFactory().fromRandom();
expect(keypair, isNot(isA<ECKeypair>()));
expect(keypair, isA<RSAKeypair>());
});

test('Generate a random RSA Keypair asynchronously', () async {
final keypair = await RSAKeypairFactory().fromRandomAsync();
expect(keypair, isNot(isA<ECKeypair>()));
expect(keypair, isA<RSAKeypair>());
});

test('Generate a random EC Keypair', () {
final keypair = ECKeypairFactory().fromRandom();
expect(keypair, isNot(isA<RSAKeypair>()));
expect(keypair, isA<ECKeypair>());
});

test('Generate a random EC Keypair asynchronously', () async {
final keypair = await ECKeypairFactory().fromRandomAsync();
expect(keypair, isNot(isA<RSAKeypair>()));
expect(keypair, isA<ECKeypair>());
});
});

group('Edge Cases', () {
late RSAKeypair rsaKeypair;

Expand All @@ -183,19 +209,20 @@ void main() {

test(
'Public key PEM-String is formatted and with a leading and trailing whitespace',
() {
var pemLeadingWhitespace = " \n${rsaKeypair.privateKey.toFormattedPEM()}";
var pemTrailingWhitespace = "${rsaKeypair.privateKey.toFormattedPEM()}\n ";
() {
var pemLeadingWhitespace = " \n${rsaKeypair.privateKey.toFormattedPEM()}";
var pemTrailingWhitespace =
"${rsaKeypair.privateKey.toFormattedPEM()}\n ";

final publicKeyString = rsaKeypair.privateKey.toString();
final publicKeyString = rsaKeypair.privateKey.toString();

final publicKeyLeadingWhitespace =
final publicKeyLeadingWhitespace =
RSAPrivateKey.fromPEM(pemLeadingWhitespace);
expect(publicKeyLeadingWhitespace.toString(), publicKeyString);
expect(publicKeyLeadingWhitespace.toString(), publicKeyString);

final publicKeyTrailingWhitespace =
final publicKeyTrailingWhitespace =
RSAPrivateKey.fromPEM(pemTrailingWhitespace);
expect(publicKeyTrailingWhitespace.toString(), publicKeyString);
});
expect(publicKeyTrailingWhitespace.toString(), publicKeyString);
});
});
}

0 comments on commit e014ad8

Please sign in to comment.