Skip to content

Commit

Permalink
Fixed an exception thrown when no source of cryptographically secure …
Browse files Browse the repository at this point in the history
…random numbers is available (#85)

Co-authored-by: Robert <[email protected]>
  • Loading branch information
robert-smartbear and Robert authored Sep 18, 2024
1 parent 4c4e075 commit 671190a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

## TBD

### Bug fixes

* Fixed an exception thrown when no source of cryptographically secure random numbers is available [85](https://github.com/bugsnag/bugsnag-flutter-performance/pull/85)

## 1.2.0 (2024-09-13)

### Enhancements
Expand Down
26 changes: 25 additions & 1 deletion packages/bugsnag_flutter_performance/lib/src/util/random.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,35 @@ BigInt randomTraceId() {
}

BigInt randomValue(int length) {
final random = Random.secure();
final random = _Random();
BigInt result = BigInt.from(random.nextInt(256) & 0xff);
for (var i = 1; i < length; i++) {
int byte = random.nextInt(256);
result = (result << 8) | BigInt.from(byte & 0xff);
}
return result;
}

class _Random {
static final _sharedRandom = Random();
late final Random? _secureRandom;

_Random() {
try {
_secureRandom = Random.secure();
} catch (e) {
// deliberately ignored
}
}

int nextInt(int max) {
try {
if (_secureRandom != null) {
return _secureRandom!.nextInt(max);
}
} catch (e) {
// deliberately ignored
}
return _sharedRandom.nextInt(max);
}
}

0 comments on commit 671190a

Please sign in to comment.