From cdf81ad44d007c662a0be4cacdd9c319bf536c6c Mon Sep 17 00:00:00 2001 From: HamdaanAliQuatil <96776914+HamdaanAliQuatil@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:16:31 +0530 Subject: [PATCH] refactor: update pbkdf2-secret-key (#151) Co-authored-by: Jonas Finnemann Jensen --- lib/src/impl_ffi/impl_ffi.dart | 3 +++ lib/src/impl_ffi/impl_ffi.pbkdf2.dart | 17 +++++++++---- lib/src/impl_interface/impl_interface.dart | 2 ++ .../impl_interface/impl_interface.pbkdf2.dart | 23 ++++++++++++++++++ lib/src/impl_js/impl_js.dart | 3 +++ lib/src/impl_js/impl_js.pbkdf2.dart | 17 +++++++++---- lib/src/impl_stub.dart | 2 -- lib/src/impl_stub/impl_stub.dart | 4 ++++ lib/src/impl_stub/impl_stub.pbkdf2.dart | 24 +++++++++++++++++++ lib/src/webcrypto/webcrypto.pbkdf2.dart | 16 ++++++++----- 10 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 lib/src/impl_interface/impl_interface.pbkdf2.dart create mode 100644 lib/src/impl_stub/impl_stub.pbkdf2.dart diff --git a/lib/src/impl_ffi/impl_ffi.dart b/lib/src/impl_ffi/impl_ffi.dart index e824e8ff..01aa633a 100644 --- a/lib/src/impl_ffi/impl_ffi.dart +++ b/lib/src/impl_ffi/impl_ffi.dart @@ -86,4 +86,7 @@ final class _WebCryptoImpl implements WebCryptoImpl { @override final hmacSecretKey = const _StaticHmacSecretKeyImpl(); + + @override + final pbkdf2SecretKey = const _StaticPbkdf2SecretKeyImpl(); } diff --git a/lib/src/impl_ffi/impl_ffi.pbkdf2.dart b/lib/src/impl_ffi/impl_ffi.pbkdf2.dart index 153eaa26..c9d4b16f 100644 --- a/lib/src/impl_ffi/impl_ffi.pbkdf2.dart +++ b/lib/src/impl_ffi/impl_ffi.pbkdf2.dart @@ -16,14 +16,23 @@ part of 'impl_ffi.dart'; -Future pbkdf2SecretKey_importRawKey(List keyData) async { - return _Pbkdf2SecretKey(Uint8List.fromList(keyData)); +Future pbkdf2SecretKey_importRawKey(List keyData) async { + return _Pbkdf2SecretKeyImpl(Uint8List.fromList(keyData)); } -class _Pbkdf2SecretKey implements Pbkdf2SecretKey { +final class _StaticPbkdf2SecretKeyImpl implements StaticPbkdf2SecretKeyImpl { + const _StaticPbkdf2SecretKeyImpl(); + + @override + Future importRawKey(List keyData) { + return pbkdf2SecretKey_importRawKey(keyData); + } +} + +final class _Pbkdf2SecretKeyImpl implements Pbkdf2SecretKeyImpl { final Uint8List _key; - _Pbkdf2SecretKey(this._key); + _Pbkdf2SecretKeyImpl(this._key); @override String toString() { diff --git a/lib/src/impl_interface/impl_interface.dart b/lib/src/impl_interface/impl_interface.dart index 27531c7e..75ccd1f9 100644 --- a/lib/src/impl_interface/impl_interface.dart +++ b/lib/src/impl_interface/impl_interface.dart @@ -23,6 +23,7 @@ import 'package:webcrypto/webcrypto.dart'; part 'impl_interface.aescbc.dart'; part 'impl_interface.aesctr.dart'; part 'impl_interface.hmac.dart'; +part 'impl_interface.pbkdf2.dart'; part 'impl_interface.aesgcm.dart'; /// Interface to be provided by platform implementations. @@ -46,4 +47,5 @@ abstract interface class WebCryptoImpl { StaticAesCtrSecretKeyImpl get aesCtrSecretKey; StaticAesGcmSecretKeyImpl get aesGcmSecretKey; StaticHmacSecretKeyImpl get hmacSecretKey; + StaticPbkdf2SecretKeyImpl get pbkdf2SecretKey; } diff --git a/lib/src/impl_interface/impl_interface.pbkdf2.dart b/lib/src/impl_interface/impl_interface.pbkdf2.dart new file mode 100644 index 00000000..26f1d1ea --- /dev/null +++ b/lib/src/impl_interface/impl_interface.pbkdf2.dart @@ -0,0 +1,23 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +part of 'impl_interface.dart'; + +abstract interface class StaticPbkdf2SecretKeyImpl { + Future importRawKey(List keyData); +} + +abstract interface class Pbkdf2SecretKeyImpl { + Future deriveBits(int length, Hash hash, List salt, int iterations); +} diff --git a/lib/src/impl_js/impl_js.dart b/lib/src/impl_js/impl_js.dart index f969fa65..5acd0e37 100644 --- a/lib/src/impl_js/impl_js.dart +++ b/lib/src/impl_js/impl_js.dart @@ -72,4 +72,7 @@ final class _WebCryptoImpl implements WebCryptoImpl { @override final hmacSecretKey = const _StaticHmacSecretKeyImpl(); + + @override + final pbkdf2SecretKey = const _StaticPbkdf2SecretKeyImpl(); } diff --git a/lib/src/impl_js/impl_js.pbkdf2.dart b/lib/src/impl_js/impl_js.pbkdf2.dart index b8479971..8f72f857 100644 --- a/lib/src/impl_js/impl_js.pbkdf2.dart +++ b/lib/src/impl_js/impl_js.pbkdf2.dart @@ -18,8 +18,8 @@ part of 'impl_js.dart'; const _pbkdf2AlgorithmName = 'PBKDF2'; -Future pbkdf2SecretKey_importRawKey(List keyData) async { - return _Pbkdf2SecretKey(await _importKey( +Future pbkdf2SecretKey_importRawKey(List keyData) async { + return _Pbkdf2SecretKeyImpl(await _importKey( 'raw', keyData, const subtle.Algorithm(name: _pbkdf2AlgorithmName), @@ -31,9 +31,18 @@ Future pbkdf2SecretKey_importRawKey(List keyData) async { )); } -class _Pbkdf2SecretKey implements Pbkdf2SecretKey { +final class _StaticPbkdf2SecretKeyImpl implements StaticPbkdf2SecretKeyImpl { + const _StaticPbkdf2SecretKeyImpl(); + + @override + Future importRawKey(List keyData) { + return pbkdf2SecretKey_importRawKey(keyData); + } +} + +final class _Pbkdf2SecretKeyImpl implements Pbkdf2SecretKeyImpl { final subtle.JSCryptoKey _key; - _Pbkdf2SecretKey(this._key); + _Pbkdf2SecretKeyImpl(this._key); @override String toString() { diff --git a/lib/src/impl_stub.dart b/lib/src/impl_stub.dart index f0455e9c..f1e3b991 100644 --- a/lib/src/impl_stub.dart +++ b/lib/src/impl_stub.dart @@ -234,5 +234,3 @@ Future hkdfSecretKey_importRawKey(List keyData) => //---------------------- PBKDF2 -Future pbkdf2SecretKey_importRawKey(List keyData) => - throw _notImplemented; diff --git a/lib/src/impl_stub/impl_stub.dart b/lib/src/impl_stub/impl_stub.dart index f6faa5e9..beeb582b 100644 --- a/lib/src/impl_stub/impl_stub.dart +++ b/lib/src/impl_stub/impl_stub.dart @@ -21,6 +21,7 @@ part 'impl_stub.aescbc.dart'; part 'impl_stub.aesctr.dart'; part 'impl_stub.aesgcm.dart'; part 'impl_stub.hmac.dart'; +part 'impl_stub.pbkdf2.dart'; const WebCryptoImpl webCryptImpl = _WebCryptoImpl(); @@ -38,4 +39,7 @@ final class _WebCryptoImpl implements WebCryptoImpl { @override final hmacSecretKey = const _StaticHmacSecretKeyImpl(); + + @override + final pbkdf2SecretKey = const _StaticPbkdf2SecretKeyImpl(); } diff --git a/lib/src/impl_stub/impl_stub.pbkdf2.dart b/lib/src/impl_stub/impl_stub.pbkdf2.dart new file mode 100644 index 00000000..e1fc586f --- /dev/null +++ b/lib/src/impl_stub/impl_stub.pbkdf2.dart @@ -0,0 +1,24 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +part of 'impl_stub.dart'; + +final class _StaticPbkdf2SecretKeyImpl implements StaticPbkdf2SecretKeyImpl { + const _StaticPbkdf2SecretKeyImpl(); + + @override + Future importRawKey(List keyData) { + throw UnimplementedError('Not implemented'); + } +} diff --git a/lib/src/webcrypto/webcrypto.pbkdf2.dart b/lib/src/webcrypto/webcrypto.pbkdf2.dart index 4c6892bf..f35fc5ee 100644 --- a/lib/src/webcrypto/webcrypto.pbkdf2.dart +++ b/lib/src/webcrypto/webcrypto.pbkdf2.dart @@ -50,17 +50,20 @@ part of 'webcrypto.dart'; /// /// [1]: https://tools.ietf.org/html/rfc8018 // TODO: Rewrite all RFC links to use https://www.rfc-editor.org/rfc/rfcXXXX -@sealed -abstract class Pbkdf2SecretKey { - Pbkdf2SecretKey._(); // keep the constructor private. + +final class Pbkdf2SecretKey { + final Pbkdf2SecretKeyImpl _impl; + + Pbkdf2SecretKey._(this._impl); // keep the constructor private. /// Import [Pbkdf2SecretKey] from raw [keyData]. /// /// Creates a [Pbkdf2SecretKey] for key derivation using [keyData]. /// /// {@macro Pbkdf2SecretKey:example} - static Future importRawKey(List keyData) { - return impl.pbkdf2SecretKey_importRawKey(keyData); + static Future importRawKey(List keyData) async { + final impl = await webCryptImpl.pbkdf2SecretKey.importRawKey(keyData); + return Pbkdf2SecretKey._(impl); } /// Derive key from [salt] and password specified as `keyData` in @@ -90,5 +93,6 @@ abstract class Pbkdf2SecretKey { Hash hash, List salt, int iterations, - ); + ) => + _impl.deriveBits(length, hash, salt, iterations); }