Skip to content

Commit 43fcf0d

Browse files
Refactor HmacSecretKey Class (#144)
* refactor: update hmac-secret-key * Update lib/src/impl_stub.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/webcrypto/webcrypto.hmac.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * feat: add unimplemented error message --------- Co-authored-by: Jonas Finnemann Jensen <[email protected]>
1 parent 1fcd062 commit 43fcf0d

File tree

10 files changed

+165
-49
lines changed

10 files changed

+165
-49
lines changed

lib/src/impl_ffi/impl_ffi.dart

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

8181
@override
8282
final aesCtrSecretKey = const _StaticAesCtrSecretKeyImpl();
83+
84+
@override
85+
final hmacSecretKey = const _StaticHmacSecretKeyImpl();
8386
}

lib/src/impl_ffi/impl_ffi.hmac.dart

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ String _hmacJwkAlgFromHash(_Hash hash) {
4949
throw UnsupportedError('hash is not supported');
5050
}
5151

52-
Future<HmacSecretKey> hmacSecretKey_importRawKey(
52+
Future<HmacSecretKeyImpl> hmacSecretKey_importRawKey(
5353
List<int> keyData,
5454
Hash hash, {
5555
int? length,
5656
}) async {
57-
return _HmacSecretKey(
57+
return _HmacSecretKeyImpl(
5858
_asUint8ListZeroedToBitLength(keyData, length),
5959
_Hash.fromHash(hash),
6060
);
6161
}
6262

63-
Future<HmacSecretKey> hmacSecretKey_importJsonWebKey(
63+
Future<HmacSecretKeyImpl> hmacSecretKey_importJsonWebKey(
6464
Map<String, dynamic> jwk,
6565
Hash hash, {
6666
int? length,
@@ -86,7 +86,7 @@ Future<HmacSecretKey> hmacSecretKey_importJsonWebKey(
8686
return hmacSecretKey_importRawKey(keyData, hash, length: length);
8787
}
8888

89-
Future<HmacSecretKey> hmacSecretKey_generateKey(
89+
Future<HmacSecretKeyImpl> hmacSecretKey_generateKey(
9090
Hash hash, {
9191
int? length,
9292
}) async {
@@ -95,25 +95,44 @@ Future<HmacSecretKey> hmacSecretKey_generateKey(
9595
final keyData = Uint8List((length / 8).ceil());
9696
fillRandomBytes(keyData);
9797

98-
return _HmacSecretKey(
98+
return _HmacSecretKeyImpl(
9999
_asUint8ListZeroedToBitLength(keyData, length),
100100
h,
101101
);
102102
}
103103

104-
class _HmacSecretKey implements HmacSecretKey {
104+
final class _StaticHmacSecretKeyImpl implements StaticHmacSecretKeyImpl {
105+
const _StaticHmacSecretKeyImpl();
106+
107+
@override
108+
Future<HmacSecretKeyImpl> importRawKey(List<int> keyData, Hash hash, {int? length}) {
109+
return hmacSecretKey_importRawKey(keyData, hash, length: length);
110+
}
111+
112+
@override
113+
Future<HmacSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk, Hash hash, {int? length}) {
114+
return hmacSecretKey_importJsonWebKey(jwk, hash, length: length);
115+
}
116+
117+
@override
118+
Future<HmacSecretKeyImpl> generateKey(Hash hash, {int? length = 32}) {
119+
return hmacSecretKey_generateKey(hash, length: length);
120+
}
121+
}
122+
123+
final class _HmacSecretKeyImpl implements HmacSecretKeyImpl {
105124
final _Hash _hash;
106125
final Uint8List _keyData;
107126

108-
_HmacSecretKey(this._keyData, this._hash);
127+
_HmacSecretKeyImpl(this._keyData, this._hash);
109128

110129
@override
111130
String toString() {
112-
return 'Instance of \'HmacSecretKey\'';
131+
return 'Instance of \'HmacSecretKeyImpl\'';
113132
}
114133

115134
@override
116-
Future<Uint8List> signBytes(List<int> data) => signStream(Stream.value(data));
135+
Future<Uint8List> signBytes(List<int> data) => signStream(Stream.value(data));
117136

118137
@override
119138
Future<Uint8List> signStream(Stream<List<int>> data) {

lib/src/impl_interface/impl_interface.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ library impl_stub;
1717
import 'dart:typed_data';
1818
import 'dart:async';
1919

20+
import 'package:webcrypto/webcrypto.dart';
21+
2022

2123
part 'impl_interface.aescbc.dart';
2224
part 'impl_interface.aesctr.dart';
25+
part 'impl_interface.hmac.dart';
2326

2427
/// Interface to be provided by platform implementations.
2528
///
@@ -40,4 +43,5 @@ part 'impl_interface.aesctr.dart';
4043
abstract interface class WebCryptoImpl {
4144
StaticAesCbcSecretKeyImpl get aesCbcSecretKey;
4245
StaticAesCtrSecretKeyImpl get aesCtrSecretKey;
46+
StaticHmacSecretKeyImpl get hmacSecretKey;
4347
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 StaticHmacSecretKeyImpl {
18+
Future<HmacSecretKeyImpl> importRawKey(List<int> keyData, Hash hash, {int? length});
19+
Future<HmacSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk, Hash hash, {int? length});
20+
Future<HmacSecretKeyImpl> generateKey(Hash hash, {int? length});
21+
}
22+
23+
abstract interface class HmacSecretKeyImpl {
24+
Future<Uint8List> signBytes(List<int> data);
25+
Future<bool> verifyBytes(List<int> signature, List<int> data);
26+
Future<Uint8List> signStream(Stream<List<int>> data);
27+
Future<bool> verifyStream(List<int> signature, Stream<List<int>> data);
28+
Future<Uint8List> exportRawKey();
29+
Future<Map<String, dynamic>> exportJsonWebKey();
30+
}

lib/src/impl_js/impl_js.dart

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

6767
@override
6868
final aesCtrSecretKey = const _StaticAesCtrSecretKeyImpl();
69+
70+
@override
71+
final hmacSecretKey = const _StaticHmacSecretKeyImpl();
6972
}

lib/src/impl_js/impl_js.hmac.dart

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

1919
const _hmacAlgorithm = subtle.Algorithm(name: 'HMAC');
2020

21-
Future<HmacSecretKey> hmacSecretKey_importRawKey(
21+
Future<HmacSecretKeyImpl> hmacSecretKey_importRawKey(
2222
List<int> keyData,
2323
Hash hash, {
2424
int? length,
2525
}) async {
26-
return _HmacSecretKey(await _importKey(
26+
return _HmacSecretKeyImpl(await _importKey(
2727
'raw',
2828
keyData,
2929
length == null
@@ -41,12 +41,12 @@ Future<HmacSecretKey> hmacSecretKey_importRawKey(
4141
));
4242
}
4343

44-
Future<HmacSecretKey> hmacSecretKey_importJsonWebKey(
44+
Future<HmacSecretKeyImpl> hmacSecretKey_importJsonWebKey(
4545
Map<String, dynamic> jwk,
4646
Hash hash, {
4747
int? length,
4848
}) async {
49-
return _HmacSecretKey(await _importJsonWebKey(
49+
return _HmacSecretKeyImpl(await _importJsonWebKey(
5050
jwk,
5151
length == null
5252
? subtle.Algorithm(
@@ -63,9 +63,9 @@ Future<HmacSecretKey> hmacSecretKey_importJsonWebKey(
6363
));
6464
}
6565

66-
Future<HmacSecretKey> hmacSecretKey_generateKey(Hash hash,
66+
Future<HmacSecretKeyImpl> hmacSecretKey_generateKey(Hash hash,
6767
{int? length}) async {
68-
return _HmacSecretKey(await _generateKey(
68+
return _HmacSecretKeyImpl(await _generateKey(
6969
length == null
7070
? subtle.Algorithm(
7171
name: 'HMAC',
@@ -81,9 +81,30 @@ Future<HmacSecretKey> hmacSecretKey_generateKey(Hash hash,
8181
));
8282
}
8383

84-
class _HmacSecretKey implements HmacSecretKey {
84+
final class _StaticHmacSecretKeyImpl implements StaticHmacSecretKeyImpl {
85+
const _StaticHmacSecretKeyImpl();
86+
87+
@override
88+
Future<HmacSecretKeyImpl> importRawKey(List<int> keyData, Hash hash,
89+
{int? length}) {
90+
return hmacSecretKey_importRawKey(keyData, hash, length: length);
91+
}
92+
93+
@override
94+
Future<HmacSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk, Hash hash,
95+
{int? length}) {
96+
return hmacSecretKey_importJsonWebKey(jwk, hash, length: length);
97+
}
98+
99+
@override
100+
Future<HmacSecretKeyImpl> generateKey(Hash hash, {int? length = 32}) {
101+
return hmacSecretKey_generateKey(hash, length: length);
102+
}
103+
}
104+
105+
final class _HmacSecretKeyImpl implements HmacSecretKeyImpl {
85106
final subtle.JSCryptoKey _key;
86-
_HmacSecretKey(this._key);
107+
_HmacSecretKeyImpl(this._key);
87108

88109
@override
89110
String toString() {

lib/src/impl_stub.dart

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,6 @@ const Hash sha256 = _UnimplementedHash();
4545
const Hash sha384 = _UnimplementedHash();
4646
const Hash sha512 = _UnimplementedHash();
4747

48-
//---------------------- HMAC
49-
Future<HmacSecretKey> hmacSecretKey_importRawKey(
50-
List<int> keyData,
51-
Hash hash, {
52-
int? length,
53-
}) =>
54-
throw _notImplemented;
55-
56-
Future<HmacSecretKey> hmacSecretKey_importJsonWebKey(
57-
Map<String, dynamic> jwk,
58-
Hash hash, {
59-
int? length,
60-
}) =>
61-
throw _notImplemented;
62-
63-
Future<HmacSecretKey> hmacSecretKey_generateKey(Hash hash, {int? length}) =>
64-
throw _notImplemented;
6548

6649
//---------------------- RSASSA_PKCS1_v1_5
6750

lib/src/impl_stub/impl_stub.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
library webcrypto.impl_stub;
1616

1717
import 'package:webcrypto/src/impl_interface/impl_interface.dart';
18+
import 'package:webcrypto/webcrypto.dart';
1819

1920
part 'impl_stub.aescbc.dart';
2021
part 'impl_stub.aesctr.dart';
22+
part 'impl_stub.hmac.dart';
2123

2224
const WebCryptoImpl webCryptImpl = _WebCryptoImpl();
2325

@@ -29,4 +31,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {
2931

3032
@override
3133
final aesCtrSecretKey = const _StaticAesCtrSecretKeyImpl();
34+
35+
@override
36+
final hmacSecretKey = const _StaticHmacSecretKeyImpl();
3237
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 _StaticHmacSecretKeyImpl implements StaticHmacSecretKeyImpl {
18+
const _StaticHmacSecretKeyImpl();
19+
20+
@override
21+
Future<HmacSecretKeyImpl> importRawKey(List<int> keyData, Hash hash, {int? length}) {
22+
throw UnimplementedError('Not implemented');
23+
}
24+
25+
@override
26+
Future<HmacSecretKeyImpl> importJsonWebKey(Map<String, dynamic> jwk, Hash hash, {int? length}) {
27+
throw UnimplementedError('Not implemented');
28+
}
29+
30+
@override
31+
Future<HmacSecretKeyImpl> generateKey(Hash hash, {int? length = 32}) {
32+
throw UnimplementedError('Not implemented');
33+
}
34+
}

0 commit comments

Comments
 (0)