Skip to content
Open
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
34 changes: 33 additions & 1 deletion lib/src/impl_ffi/impl_ffi.pbkdf2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ final class _Pbkdf2SecretKeyImpl implements Pbkdf2SecretKeyImpl {

return _Scope.async((scope) async {
final out = scope<ffi.Uint8>(lengthInBytes);
_checkOpIsOne(ssl.PKCS5_PBKDF2_HMAC(
_checkOpIsOne(await _PKCS5_PBKDF2_HMAC(
scope.dataAsPointer(_key),
_key.length,
scope.dataAsPointer(salt),
Expand All @@ -85,3 +85,35 @@ final class _Pbkdf2SecretKeyImpl implements Pbkdf2SecretKeyImpl {
});
}
}

/// Helper function to run `ssl.PKCS5_PBKDF2_HMAC` in an [Isolate]
/// using [Isolate.run].
///
/// This offloads the computationally expensive PBKDF2 operation to a
/// separate isolate to avoid blocking the main isolate.
///
/// Using this auxiliary function to wrap the call should reduce the risk that
/// unnecessary variables are copied into the closure passed to [Isolate.run].
Future<int> _PKCS5_PBKDF2_HMAC(
ffi.Pointer<ffi.Char> key,
int keyLength,
ffi.Pointer<ffi.Uint8> salt,
int saltLength,
int iterations,
ffi.Pointer<EVP_MD> md,
int lengthInBytes,
ffi.Pointer<ffi.Uint8> out,
) async =>
await Isolate.run(
() => ssl.PKCS5_PBKDF2_HMAC(
key,
keyLength,
salt,
saltLength,
iterations,
md,
lengthInBytes,
out,
),
debugName: 'PKCS5_PBKDF2_HMAC',
);
Loading