Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/chrome.workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
run: dart pub get

- name: Test chrome
run: dart test -j 1 -p chrome
run: dart test --timeout 2x -j 1 -p chrome
40 changes: 33 additions & 7 deletions lib/src/platform_check/node_crypto.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
/// Wrapper for needed NodeJS Crypto library function and require.
library nodecryto;
library nodecrypto;

import 'dart:js_interop';
import 'dart:js_interop_unsafe';

const bool isDart2JS = bool.fromEnvironment('dart.tool.dart2js');

@JS()
@staticInterop
class Process {}

@JS()
external JSObject require(String id);
@staticInterop
class Versions {}

@JS('process')
external Process? get _process;

extension on Process {
external Versions? get versions;
}

extension on Versions {
external JSAny get node;
}

bool get isNodeDart2JS => _process?.versions?.node != null && isDart2JS;

@JS()
@staticInterop
class Crypto {}

extension on Crypto {
external JSUint8Array randomBytes(int size);
}

@JS()
external Crypto require(String id);

class NodeCrypto {
static JSAny randomFillSync(JSAny buf) {
final crypto = require('crypto');
return crypto.callMethod('randomFillSync'.toJS, buf);
}
static JSUint8Array randomBytes(int size) =>
require('crypto').randomBytes(size);
}
37 changes: 10 additions & 27 deletions lib/src/platform_check/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,9 @@ import 'platform_check.dart';

class PlatformWeb extends Platform {
static final PlatformWeb instance = PlatformWeb();
static bool useBuiltInRng = false;
static bool useBuiltInRng = !isNodeDart2JS;

PlatformWeb() {
try {
Random.secure();
useBuiltInRng = true;
} on UnsupportedError {
useBuiltInRng = false;
}
}
const PlatformWeb();

@override
bool get isNative => false;
Expand All @@ -28,17 +21,8 @@ class PlatformWeb extends Platform {
String get platform => 'web';

@override
EntropySource platformEntropySource() {
if (useBuiltInRng) {
return _JsBuiltInEntropySource();
} else {
//
// Assume that if we cannot get a built in Secure RNG then we are
// probably on NodeJS.
//
return _JsNodeEntropySource();
}
}
EntropySource platformEntropySource() =>
useBuiltInRng ? _JsBuiltInEntropySource() : _JsNodeEntropySource();
}

// Uses the built in entropy source
Expand All @@ -47,19 +31,18 @@ class _JsBuiltInEntropySource implements EntropySource {

@override
Uint8List getBytes(int len) {
return Uint8List.fromList(
List<int>.generate(len, (i) => _src.nextInt(256)));
final Uint8List bytes = Uint8List(len);
for (int i = 0; i < len; i++) {
bytes[i] = _src.nextInt(256);
}
return bytes;
}
}

///
class _JsNodeEntropySource implements EntropySource {
@override
Uint8List getBytes(int len) {
var list = Uint8List(len);
NodeCrypto.randomFillSync(list.buffer.toJS);
return list;
}
Uint8List getBytes(int len) => NodeCrypto.randomBytes(len).toDart;
}

Platform getPlatform() => PlatformWeb.instance;