Skip to content

Commit

Permalink
refactor: update pbkdf2-secret-key (#151)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Finnemann Jensen <[email protected]>
  • Loading branch information
HamdaanAliQuatil and jonasfj authored Oct 11, 2024
1 parent f4de7c1 commit cdf81ad
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 16 deletions.
3 changes: 3 additions & 0 deletions lib/src/impl_ffi/impl_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {

@override
final hmacSecretKey = const _StaticHmacSecretKeyImpl();

@override
final pbkdf2SecretKey = const _StaticPbkdf2SecretKeyImpl();
}
17 changes: 13 additions & 4 deletions lib/src/impl_ffi/impl_ffi.pbkdf2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@

part of 'impl_ffi.dart';

Future<Pbkdf2SecretKey> pbkdf2SecretKey_importRawKey(List<int> keyData) async {
return _Pbkdf2SecretKey(Uint8List.fromList(keyData));
Future<Pbkdf2SecretKeyImpl> pbkdf2SecretKey_importRawKey(List<int> keyData) async {
return _Pbkdf2SecretKeyImpl(Uint8List.fromList(keyData));
}

class _Pbkdf2SecretKey implements Pbkdf2SecretKey {
final class _StaticPbkdf2SecretKeyImpl implements StaticPbkdf2SecretKeyImpl {
const _StaticPbkdf2SecretKeyImpl();

@override
Future<Pbkdf2SecretKeyImpl> importRawKey(List<int> keyData) {
return pbkdf2SecretKey_importRawKey(keyData);
}
}

final class _Pbkdf2SecretKeyImpl implements Pbkdf2SecretKeyImpl {
final Uint8List _key;

_Pbkdf2SecretKey(this._key);
_Pbkdf2SecretKeyImpl(this._key);

@override
String toString() {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/impl_interface/impl_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -46,4 +47,5 @@ abstract interface class WebCryptoImpl {
StaticAesCtrSecretKeyImpl get aesCtrSecretKey;
StaticAesGcmSecretKeyImpl get aesGcmSecretKey;
StaticHmacSecretKeyImpl get hmacSecretKey;
StaticPbkdf2SecretKeyImpl get pbkdf2SecretKey;
}
23 changes: 23 additions & 0 deletions lib/src/impl_interface/impl_interface.pbkdf2.dart
Original file line number Diff line number Diff line change
@@ -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<Pbkdf2SecretKeyImpl> importRawKey(List<int> keyData);
}

abstract interface class Pbkdf2SecretKeyImpl {
Future<Uint8List> deriveBits(int length, Hash hash, List<int> salt, int iterations);
}
3 changes: 3 additions & 0 deletions lib/src/impl_js/impl_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {

@override
final hmacSecretKey = const _StaticHmacSecretKeyImpl();

@override
final pbkdf2SecretKey = const _StaticPbkdf2SecretKeyImpl();
}
17 changes: 13 additions & 4 deletions lib/src/impl_js/impl_js.pbkdf2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ part of 'impl_js.dart';

const _pbkdf2AlgorithmName = 'PBKDF2';

Future<Pbkdf2SecretKey> pbkdf2SecretKey_importRawKey(List<int> keyData) async {
return _Pbkdf2SecretKey(await _importKey(
Future<Pbkdf2SecretKeyImpl> pbkdf2SecretKey_importRawKey(List<int> keyData) async {
return _Pbkdf2SecretKeyImpl(await _importKey(
'raw',
keyData,
const subtle.Algorithm(name: _pbkdf2AlgorithmName),
Expand All @@ -31,9 +31,18 @@ Future<Pbkdf2SecretKey> pbkdf2SecretKey_importRawKey(List<int> keyData) async {
));
}

class _Pbkdf2SecretKey implements Pbkdf2SecretKey {
final class _StaticPbkdf2SecretKeyImpl implements StaticPbkdf2SecretKeyImpl {
const _StaticPbkdf2SecretKeyImpl();

@override
Future<Pbkdf2SecretKeyImpl> importRawKey(List<int> keyData) {
return pbkdf2SecretKey_importRawKey(keyData);
}
}

final class _Pbkdf2SecretKeyImpl implements Pbkdf2SecretKeyImpl {
final subtle.JSCryptoKey _key;
_Pbkdf2SecretKey(this._key);
_Pbkdf2SecretKeyImpl(this._key);

@override
String toString() {
Expand Down
2 changes: 0 additions & 2 deletions lib/src/impl_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,3 @@ Future<HkdfSecretKey> hkdfSecretKey_importRawKey(List<int> keyData) =>

//---------------------- PBKDF2

Future<Pbkdf2SecretKey> pbkdf2SecretKey_importRawKey(List<int> keyData) =>
throw _notImplemented;
4 changes: 4 additions & 0 deletions lib/src/impl_stub/impl_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -38,4 +39,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {

@override
final hmacSecretKey = const _StaticHmacSecretKeyImpl();

@override
final pbkdf2SecretKey = const _StaticPbkdf2SecretKeyImpl();
}
24 changes: 24 additions & 0 deletions lib/src/impl_stub/impl_stub.pbkdf2.dart
Original file line number Diff line number Diff line change
@@ -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<Pbkdf2SecretKeyImpl> importRawKey(List<int> keyData) {
throw UnimplementedError('Not implemented');
}
}
16 changes: 10 additions & 6 deletions lib/src/webcrypto/webcrypto.pbkdf2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pbkdf2SecretKey> importRawKey(List<int> keyData) {
return impl.pbkdf2SecretKey_importRawKey(keyData);
static Future<Pbkdf2SecretKey> importRawKey(List<int> keyData) async {
final impl = await webCryptImpl.pbkdf2SecretKey.importRawKey(keyData);
return Pbkdf2SecretKey._(impl);
}

/// Derive key from [salt] and password specified as `keyData` in
Expand Down Expand Up @@ -90,5 +93,6 @@ abstract class Pbkdf2SecretKey {
Hash hash,
List<int> salt,
int iterations,
);
) =>
_impl.deriveBits(length, hash, salt, iterations);
}

0 comments on commit cdf81ad

Please sign in to comment.