Skip to content

Commit cdf81ad

Browse files
refactor: update pbkdf2-secret-key (#151)
Co-authored-by: Jonas Finnemann Jensen <[email protected]>
1 parent f4de7c1 commit cdf81ad

File tree

10 files changed

+95
-16
lines changed

10 files changed

+95
-16
lines changed

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {
8686

8787
@override
8888
final hmacSecretKey = const _StaticHmacSecretKeyImpl();
89+
90+
@override
91+
final pbkdf2SecretKey = const _StaticPbkdf2SecretKeyImpl();
8992
}

lib/src/impl_ffi/impl_ffi.pbkdf2.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@
1616

1717
part of 'impl_ffi.dart';
1818

19-
Future<Pbkdf2SecretKey> pbkdf2SecretKey_importRawKey(List<int> keyData) async {
20-
return _Pbkdf2SecretKey(Uint8List.fromList(keyData));
19+
Future<Pbkdf2SecretKeyImpl> pbkdf2SecretKey_importRawKey(List<int> keyData) async {
20+
return _Pbkdf2SecretKeyImpl(Uint8List.fromList(keyData));
2121
}
2222

23-
class _Pbkdf2SecretKey implements Pbkdf2SecretKey {
23+
final class _StaticPbkdf2SecretKeyImpl implements StaticPbkdf2SecretKeyImpl {
24+
const _StaticPbkdf2SecretKeyImpl();
25+
26+
@override
27+
Future<Pbkdf2SecretKeyImpl> importRawKey(List<int> keyData) {
28+
return pbkdf2SecretKey_importRawKey(keyData);
29+
}
30+
}
31+
32+
final class _Pbkdf2SecretKeyImpl implements Pbkdf2SecretKeyImpl {
2433
final Uint8List _key;
2534

26-
_Pbkdf2SecretKey(this._key);
35+
_Pbkdf2SecretKeyImpl(this._key);
2736

2837
@override
2938
String toString() {

lib/src/impl_interface/impl_interface.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'package:webcrypto/webcrypto.dart';
2323
part 'impl_interface.aescbc.dart';
2424
part 'impl_interface.aesctr.dart';
2525
part 'impl_interface.hmac.dart';
26+
part 'impl_interface.pbkdf2.dart';
2627
part 'impl_interface.aesgcm.dart';
2728

2829
/// Interface to be provided by platform implementations.
@@ -46,4 +47,5 @@ abstract interface class WebCryptoImpl {
4647
StaticAesCtrSecretKeyImpl get aesCtrSecretKey;
4748
StaticAesGcmSecretKeyImpl get aesGcmSecretKey;
4849
StaticHmacSecretKeyImpl get hmacSecretKey;
50+
StaticPbkdf2SecretKeyImpl get pbkdf2SecretKey;
4951
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
part of 'impl_interface.dart';
16+
17+
abstract interface class StaticPbkdf2SecretKeyImpl {
18+
Future<Pbkdf2SecretKeyImpl> importRawKey(List<int> keyData);
19+
}
20+
21+
abstract interface class Pbkdf2SecretKeyImpl {
22+
Future<Uint8List> deriveBits(int length, Hash hash, List<int> salt, int iterations);
23+
}

lib/src/impl_js/impl_js.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {
7272

7373
@override
7474
final hmacSecretKey = const _StaticHmacSecretKeyImpl();
75+
76+
@override
77+
final pbkdf2SecretKey = const _StaticPbkdf2SecretKeyImpl();
7578
}

lib/src/impl_js/impl_js.pbkdf2.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ part of 'impl_js.dart';
1818

1919
const _pbkdf2AlgorithmName = 'PBKDF2';
2020

21-
Future<Pbkdf2SecretKey> pbkdf2SecretKey_importRawKey(List<int> keyData) async {
22-
return _Pbkdf2SecretKey(await _importKey(
21+
Future<Pbkdf2SecretKeyImpl> pbkdf2SecretKey_importRawKey(List<int> keyData) async {
22+
return _Pbkdf2SecretKeyImpl(await _importKey(
2323
'raw',
2424
keyData,
2525
const subtle.Algorithm(name: _pbkdf2AlgorithmName),
@@ -31,9 +31,18 @@ Future<Pbkdf2SecretKey> pbkdf2SecretKey_importRawKey(List<int> keyData) async {
3131
));
3232
}
3333

34-
class _Pbkdf2SecretKey implements Pbkdf2SecretKey {
34+
final class _StaticPbkdf2SecretKeyImpl implements StaticPbkdf2SecretKeyImpl {
35+
const _StaticPbkdf2SecretKeyImpl();
36+
37+
@override
38+
Future<Pbkdf2SecretKeyImpl> importRawKey(List<int> keyData) {
39+
return pbkdf2SecretKey_importRawKey(keyData);
40+
}
41+
}
42+
43+
final class _Pbkdf2SecretKeyImpl implements Pbkdf2SecretKeyImpl {
3544
final subtle.JSCryptoKey _key;
36-
_Pbkdf2SecretKey(this._key);
45+
_Pbkdf2SecretKeyImpl(this._key);
3746

3847
@override
3948
String toString() {

lib/src/impl_stub.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,3 @@ Future<HkdfSecretKey> hkdfSecretKey_importRawKey(List<int> keyData) =>
234234

235235
//---------------------- PBKDF2
236236

237-
Future<Pbkdf2SecretKey> pbkdf2SecretKey_importRawKey(List<int> keyData) =>
238-
throw _notImplemented;

lib/src/impl_stub/impl_stub.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ part 'impl_stub.aescbc.dart';
2121
part 'impl_stub.aesctr.dart';
2222
part 'impl_stub.aesgcm.dart';
2323
part 'impl_stub.hmac.dart';
24+
part 'impl_stub.pbkdf2.dart';
2425

2526
const WebCryptoImpl webCryptImpl = _WebCryptoImpl();
2627

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

3940
@override
4041
final hmacSecretKey = const _StaticHmacSecretKeyImpl();
42+
43+
@override
44+
final pbkdf2SecretKey = const _StaticPbkdf2SecretKeyImpl();
4145
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
part of 'impl_stub.dart';
16+
17+
final class _StaticPbkdf2SecretKeyImpl implements StaticPbkdf2SecretKeyImpl {
18+
const _StaticPbkdf2SecretKeyImpl();
19+
20+
@override
21+
Future<Pbkdf2SecretKeyImpl> importRawKey(List<int> keyData) {
22+
throw UnimplementedError('Not implemented');
23+
}
24+
}

lib/src/webcrypto/webcrypto.pbkdf2.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,20 @@ part of 'webcrypto.dart';
5050
///
5151
/// [1]: https://tools.ietf.org/html/rfc8018
5252
// TODO: Rewrite all RFC links to use https://www.rfc-editor.org/rfc/rfcXXXX
53-
@sealed
54-
abstract class Pbkdf2SecretKey {
55-
Pbkdf2SecretKey._(); // keep the constructor private.
53+
54+
final class Pbkdf2SecretKey {
55+
final Pbkdf2SecretKeyImpl _impl;
56+
57+
Pbkdf2SecretKey._(this._impl); // keep the constructor private.
5658

5759
/// Import [Pbkdf2SecretKey] from raw [keyData].
5860
///
5961
/// Creates a [Pbkdf2SecretKey] for key derivation using [keyData].
6062
///
6163
/// {@macro Pbkdf2SecretKey:example}
62-
static Future<Pbkdf2SecretKey> importRawKey(List<int> keyData) {
63-
return impl.pbkdf2SecretKey_importRawKey(keyData);
64+
static Future<Pbkdf2SecretKey> importRawKey(List<int> keyData) async {
65+
final impl = await webCryptImpl.pbkdf2SecretKey.importRawKey(keyData);
66+
return Pbkdf2SecretKey._(impl);
6467
}
6568

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

0 commit comments

Comments
 (0)