Skip to content

Commit d3af452

Browse files
Refactor AesGcmSecretKey Class (#143)
* refactor: update aesgcm-secret-key
1 parent 43fcf0d commit d3af452

15 files changed

+149
-42
lines changed

lib/src/impl_ffi/impl_ffi.aescbc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ final class _StaticAesCbcSecretKeyImpl implements StaticAesCbcSecretKeyImpl {
114114
}
115115
}
116116

117-
final class _AesCbcSecretKeyImpl extends AesCbcSecretKeyImpl {
117+
final class _AesCbcSecretKeyImpl implements AesCbcSecretKeyImpl {
118118
final Uint8List _key;
119119
_AesCbcSecretKeyImpl(this._key);
120120

lib/src/impl_ffi/impl_ffi.aesctr.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ final class _StaticAesCtrSecretKeyImpl implements StaticAesCtrSecretKeyImpl {
221221
}
222222
}
223223

224-
final class _AesCtrSecretKeyImpl extends AesCtrSecretKeyImpl {
224+
final class _AesCtrSecretKeyImpl implements AesCtrSecretKeyImpl {
225225
final Uint8List _key;
226226
_AesCtrSecretKeyImpl(this._key);
227227

lib/src/impl_ffi/impl_ffi.aesgcm.dart

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616

1717
part of 'impl_ffi.dart';
1818

19-
Future<AesGcmSecretKey> aesGcm_importRawKey(List<int> keyData) async =>
20-
_AesGcmSecretKey(_aesImportRawKey(keyData));
19+
Future<AesGcmSecretKeyImpl> aesGcm_importRawKey(List<int> keyData) async =>
20+
_AesGcmSecretKeyImpl(_aesImportRawKey(keyData));
2121

22-
Future<AesGcmSecretKey> aesGcm_importJsonWebKey(
22+
Future<AesGcmSecretKeyImpl> aesGcm_importJsonWebKey(
2323
Map<String, dynamic> jwk,
2424
) async =>
25-
_AesGcmSecretKey(_aesImportJwkKey(
25+
_AesGcmSecretKeyImpl(_aesImportJwkKey(
2626
jwk,
2727
expectedJwkAlgSuffix: 'GCM',
2828
));
2929

30-
Future<AesGcmSecretKey> aesGcm_generateKey(int length) async =>
31-
_AesGcmSecretKey(_aesGenerateKey(length));
30+
Future<AesGcmSecretKeyImpl> aesGcm_generateKey(int length) async =>
31+
_AesGcmSecretKeyImpl(_aesGenerateKey(length));
3232

3333
Future<Uint8List> _aesGcmEncryptDecrypt(
3434
List<int> key,
@@ -111,9 +111,28 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
111111
});
112112
}
113113

114-
class _AesGcmSecretKey implements AesGcmSecretKey {
114+
final class _StaticAesGcmSecretKeyImpl implements StaticAesGcmSecretKeyImpl {
115+
const _StaticAesGcmSecretKeyImpl();
116+
117+
@override
118+
Future<AesGcmSecretKeyImpl> importRawKey(List<int> keyData) async {
119+
return await aesGcm_importRawKey(keyData);
120+
}
121+
122+
@override
123+
Future<AesGcmSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk) async {
124+
return await aesGcm_importJsonWebKey(jwk);
125+
}
126+
127+
@override
128+
Future<AesGcmSecretKeyImpl> generateKey(int length) async {
129+
return await aesGcm_generateKey(length);
130+
}
131+
}
132+
133+
final class _AesGcmSecretKeyImpl implements AesGcmSecretKeyImpl {
115134
final Uint8List _key;
116-
_AesGcmSecretKey(this._key);
135+
_AesGcmSecretKeyImpl(this._key);
117136

118137
@override
119138
String toString() {

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ final class _WebCryptoImpl implements WebCryptoImpl {
8181
@override
8282
final aesCtrSecretKey = const _StaticAesCtrSecretKeyImpl();
8383

84+
@override
85+
final aesGcmSecretKey = const _StaticAesGcmSecretKeyImpl();
86+
8487
@override
8588
final hmacSecretKey = const _StaticHmacSecretKeyImpl();
8689
}

lib/src/impl_interface/impl_interface.aescbc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract interface class StaticAesCbcSecretKeyImpl {
2020
Future<AesCbcSecretKeyImpl> generateKey(int length);
2121
}
2222

23-
abstract class AesCbcSecretKeyImpl {
23+
abstract interface class AesCbcSecretKeyImpl {
2424
Future<Uint8List> encryptBytes(List<int> data, List<int> iv);
2525
Future<Uint8List> decryptBytes(List<int> data, List<int> iv);
2626
Stream<Uint8List> encryptStream(Stream<List<int>> data, List<int> iv);

lib/src/impl_interface/impl_interface.aesctr.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract interface class StaticAesCtrSecretKeyImpl {
2020
Future<AesCtrSecretKeyImpl> generateKey(int length);
2121
}
2222

23-
abstract class AesCtrSecretKeyImpl {
23+
abstract interface class AesCtrSecretKeyImpl {
2424
Future<Uint8List> encryptBytes(List<int> data, List<int> counter, int length);
2525
Future<Uint8List> decryptBytes(List<int> data, List<int> counter, int length);
2626
Stream<Uint8List> encryptStream(Stream<List<int>> data, List<int> counter, int length);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 StaticAesGcmSecretKeyImpl {
18+
Future<AesGcmSecretKeyImpl> importRawKey(List<int> keyData);
19+
Future<AesGcmSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk);
20+
Future<AesGcmSecretKeyImpl> generateKey(int length);
21+
}
22+
23+
abstract interface class AesGcmSecretKeyImpl {
24+
Future<Uint8List> encryptBytes(List<int> data, List<int> iv, {List<int>? additionalData, int? tagLength});
25+
Future<Uint8List> decryptBytes(List<int> data, List<int> iv, {List<int>? additionalData, int? tagLength});
26+
Future<Uint8List> exportRawKey();
27+
Future<Map<String, dynamic>> exportJsonWebKey();
28+
}

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.aesgcm.dart';
2627

2728
/// Interface to be provided by platform implementations.
2829
///
@@ -43,5 +44,6 @@ part 'impl_interface.hmac.dart';
4344
abstract interface class WebCryptoImpl {
4445
StaticAesCbcSecretKeyImpl get aesCbcSecretKey;
4546
StaticAesCtrSecretKeyImpl get aesCtrSecretKey;
47+
StaticAesGcmSecretKeyImpl get aesGcmSecretKey;
4648
StaticHmacSecretKeyImpl get hmacSecretKey;
4749
}

lib/src/impl_js/impl_js.aesctr.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ final class _StaticAesCtrSecretKeyImpl implements StaticAesCtrSecretKeyImpl {
6666
}
6767
}
6868

69-
final class _AesCtrSecretKeyImpl extends AesCtrSecretKeyImpl {
69+
final class _AesCtrSecretKeyImpl implements AesCtrSecretKeyImpl {
7070
final subtle.JSCryptoKey _key;
7171
_AesCtrSecretKeyImpl(this._key);
7272

lib/src/impl_js/impl_js.aesgcm.dart

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

1919
const _aesGcmAlgorithm = subtle.Algorithm(name: 'AES-GCM');
2020

21-
Future<AesGcmSecretKey> aesGcm_importRawKey(List<int> keyData) async {
22-
return _AesGcmSecretKey(await _importKey(
21+
Future<AesGcmSecretKeyImpl> aesGcm_importRawKey(List<int> keyData) async {
22+
return _AesGcmSecretKeyImpl(await _importKey(
2323
'raw',
2424
keyData,
2525
_aesGcmAlgorithm,
@@ -28,28 +28,47 @@ Future<AesGcmSecretKey> aesGcm_importRawKey(List<int> keyData) async {
2828
));
2929
}
3030

31-
Future<AesGcmSecretKey> aesGcm_importJsonWebKey(
31+
Future<AesGcmSecretKeyImpl> aesGcm_importJsonWebKey(
3232
Map<String, dynamic> jwk,
3333
) async {
34-
return _AesGcmSecretKey(await _importJsonWebKey(
34+
return _AesGcmSecretKeyImpl(await _importJsonWebKey(
3535
jwk,
3636
_aesGcmAlgorithm,
3737
_usagesEncryptDecrypt,
3838
'secret',
3939
));
4040
}
4141

42-
Future<AesGcmSecretKey> aesGcm_generateKey(int length) async {
43-
return _AesGcmSecretKey(await _generateKey(
42+
Future<AesGcmSecretKeyImpl> aesGcm_generateKey(int length) async {
43+
return _AesGcmSecretKeyImpl(await _generateKey(
4444
_aesGcmAlgorithm.update(length: length),
4545
_usagesEncryptDecrypt,
4646
'secret',
4747
));
4848
}
4949

50-
class _AesGcmSecretKey implements AesGcmSecretKey {
50+
final class _StaticAesGcmSecretKeyImpl implements StaticAesGcmSecretKeyImpl {
51+
const _StaticAesGcmSecretKeyImpl();
52+
53+
@override
54+
Future<AesGcmSecretKeyImpl> importRawKey(List<int> keyData) async {
55+
return await aesGcm_importRawKey(keyData);
56+
}
57+
58+
@override
59+
Future<AesGcmSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk) async {
60+
return await aesGcm_importJsonWebKey(jwk);
61+
}
62+
63+
@override
64+
Future<AesGcmSecretKeyImpl> generateKey(int length) async {
65+
return await aesGcm_generateKey(length);
66+
}
67+
}
68+
69+
final class _AesGcmSecretKeyImpl implements AesGcmSecretKeyImpl {
5170
final subtle.JSCryptoKey _key;
52-
_AesGcmSecretKey(this._key);
71+
_AesGcmSecretKeyImpl(this._key);
5372

5473
@override
5574
String toString() {

0 commit comments

Comments
 (0)