Skip to content
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

Added a SecureRandom implementation based on darts own Random class. #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Shammah Chancellor <https://github.com/schancel>
Rick Bellens <https://github.com/rbellens>
Deniz Türkoglu <https://github.com/denizt>
Yurii Baryshev <https://github.com/YuriiBaryshev>
Eric Prokop und Nils Wieler Hard- und Softwareentwicklung GbR <https://github.com/EPNW>
1 change: 1 addition & 0 deletions lib/export.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export 'package:pointycastle/paddings/iso7816d4.dart';
export 'package:pointycastle/random/auto_seed_block_ctr_random.dart';
export 'package:pointycastle/random/block_ctr_random.dart';
export 'package:pointycastle/random/fortuna_random.dart';
export 'package:pointycastle/random/dart_secure_random.dart';

// signers
export 'package:pointycastle/signers/ecdsa_signer.dart';
Expand Down
48 changes: 48 additions & 0 deletions lib/random/dart_secure_random.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// See file LICENSE for more information.

library impl.secure_random.dart_secure_random;

import 'dart:math';

import 'package:pointycastle/api.dart';
import 'package:pointycastle/src/impl/secure_random_base.dart';
import 'package:pointycastle/src/registry/registry.dart';

/// An implementation of [SecureRandom] that uses darts built-in
/// [Random] to generate random bytes.
///
/// This implementation does not give any security guarantees, but
/// trusts the dart [Random] implementation to be cryptographically secure.
class DartSecureRandom extends SecureRandomBase implements SecureRandom {
static const String _algorithmName = 'DartSecure';
static final FactoryConfig factoryConfig = StaticFactoryConfig(
SecureRandom, _algorithmName, () => DartSecureRandom());

final Random _random;

/// A newly created secure dart [Random] is used for byte generation.
DartSecureRandom() : _random = Random.secure();

/// Uses an insecure dart [Random]. This constructor should not be used
/// in production!
///
/// It is intended to be used during development when
/// generating many cryptographically secure numbers takes
/// to much time.
DartSecureRandom.insecure([int? seed]) : _random = Random(seed);

/// Uses an explicitly given dart [Random] for all operations.
DartSecureRandom.withRandom(Random random) : _random = random;

@override
String get algorithmName => _algorithmName;

@override
int nextUint8() {
return _random.nextInt(256);
}

/// The dart [Random] can not be seeded, so this is a no-op.
@override
void seed(CipherParameters params) {}
}
2 changes: 2 additions & 0 deletions lib/src/registry/registration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import 'package:pointycastle/paddings/pkcs7.dart';
import 'package:pointycastle/random/auto_seed_block_ctr_random.dart';
import 'package:pointycastle/random/block_ctr_random.dart';
import 'package:pointycastle/random/fortuna_random.dart';
import 'package:pointycastle/random/dart_secure_random.dart';
import 'package:pointycastle/signers/ecdsa_signer.dart';
import 'package:pointycastle/signers/rsa_signer.dart';
import 'package:pointycastle/src/registry/registry.dart';
Expand Down Expand Up @@ -239,6 +240,7 @@ void _registerRandoms(FactoryRegistry registry) {
registry.register(AutoSeedBlockCtrRandom.factoryConfig);
registry.register(BlockCtrRandom.factoryConfig);
registry.register(FortunaRandom.factoryConfig);
registry.register(DartSecureRandom.factoryConfig);
}

void _registerSigners(FactoryRegistry registry) {
Expand Down
2 changes: 2 additions & 0 deletions test/all_tests_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import 'random/auto_seed_block_ctr_random_test.dart'
as auto_seed_block_ctr_random_test;
import 'random/block_ctr_random_test.dart' as block_ctr_random_test;
import 'random/fortuna_random_test.dart' as fortuna_random_test;
import 'random/dart_secure_random_test.dart' as dart_secure_random_test;
import 'random/fixed_rng_test.dart' as fixed_rng_test;
import 'signers/ecdsa_signer_test.dart' as ecdsa_signer_test;
import 'signers/pss_signer_test.dart' as pss_signer_test;
Expand Down Expand Up @@ -110,6 +111,7 @@ void main() {
auto_seed_block_ctr_random_test.main();
block_ctr_random_test.main();
fortuna_random_test.main();
dart_secure_random_test.main();
fixed_rng_test.main();
ecdsa_signer_test.main();
rsa_signer_test.main();
Expand Down
25 changes: 25 additions & 0 deletions test/random/dart_secure_random_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// See file LICENSE for more information.

library test.random.dart_secure_random_test;

import 'package:pointycastle/pointycastle.dart';

import 'package:test/test.dart';

void main() {
group('DartSecure:', () {
final rnd = SecureRandom('DartSecure');

test('${rnd.algorithmName}', () {
var randomBytes = rnd.nextBytes(256);
var allZero = true;
for (var i = 0; i < randomBytes.length; i++) {
if (randomBytes[i] != 0) {
allZero = false;
break;
}
}
expect(allZero, false);
});
});
}