diff --git a/lib/src/impl_ffi/impl_ffi.pbkdf2.dart b/lib/src/impl_ffi/impl_ffi.pbkdf2.dart index 9a88d3b4..b64b00dd 100644 --- a/lib/src/impl_ffi/impl_ffi.pbkdf2.dart +++ b/lib/src/impl_ffi/impl_ffi.pbkdf2.dart @@ -71,7 +71,7 @@ final class _Pbkdf2SecretKeyImpl implements Pbkdf2SecretKeyImpl { return _Scope.async((scope) async { final out = scope(lengthInBytes); - _checkOpIsOne(ssl.PKCS5_PBKDF2_HMAC( + _checkOpIsOne(await _PKCS5_PBKDF2_HMAC( scope.dataAsPointer(_key), _key.length, scope.dataAsPointer(salt), @@ -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 _PKCS5_PBKDF2_HMAC( + ffi.Pointer key, + int keyLength, + ffi.Pointer salt, + int saltLength, + int iterations, + ffi.Pointer md, + int lengthInBytes, + ffi.Pointer out, +) async => + await Isolate.run( + () => ssl.PKCS5_PBKDF2_HMAC( + key, + keyLength, + salt, + saltLength, + iterations, + md, + lengthInBytes, + out, + ), + debugName: 'PKCS5_PBKDF2_HMAC', + );