Skip to content

Commit fe6e923

Browse files
Update fillRandomBytes (#172)
* refactor: update fill-random-bytes * Update lib/src/impl_interface/impl_interface.random.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * fix: replace webcrypt with subtle.window * fix: update uint8list: too long test values * tests: update test error details * refactor: format dart code * fix: update tests in group crypto * chore: add missing import --------- Co-authored-by: Jonas Finnemann Jensen <[email protected]> Co-authored-by: Jonas Finnemann Jensen <[email protected]>
1 parent 007d655 commit fe6e923

13 files changed

+94
-74
lines changed

lib/src/impl_ffi/impl_ffi.dart

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

113113
@override
114114
final sha512 = const _Sha512();
115+
116+
@override
117+
final random = const _RandomImpl();
115118
}

lib/src/impl_ffi/impl_ffi.random.dart

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@
1414

1515
part of 'impl_ffi.dart';
1616

17-
void fillRandomBytes(TypedData destination) {
18-
return _Scope.sync((scope) {
19-
final dest = destination.buffer.asUint8List(
20-
destination.offsetInBytes,
21-
destination.lengthInBytes,
22-
);
17+
final class _RandomImpl implements RandomImpl {
18+
const _RandomImpl();
2319

24-
final out = scope<ffi.Uint8>(dest.length);
25-
_checkOp(ssl.RAND_bytes(out, dest.length) == 1);
26-
dest.setAll(0, out.asTypedList(dest.length));
27-
});
20+
@override
21+
void fillRandomBytes(TypedData destination) {
22+
return _Scope.sync((scope) {
23+
final dest = destination.buffer.asUint8List(
24+
destination.offsetInBytes,
25+
destination.lengthInBytes,
26+
);
27+
28+
final out = scope<ffi.Uint8>(dest.length);
29+
_checkOp(ssl.RAND_bytes(out, dest.length) == 1);
30+
dest.setAll(0, out.asTypedList(dest.length));
31+
});
32+
}
2833
}

lib/src/impl_interface/impl_interface.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ part 'impl_interface.hkdf.dart';
3131
part 'impl_interface.rsapss.dart';
3232
part 'impl_interface.rsassapkcs1v15.dart';
3333
part 'impl_interface.digest.dart';
34+
part 'impl_interface.random.dart';
3435

3536
/// A key-pair as returned from key generation.
3637
class KeyPair<S, T> {
@@ -115,4 +116,5 @@ abstract interface class WebCryptoImpl {
115116
HashImpl get sha256;
116117
HashImpl get sha384;
117118
HashImpl get sha512;
119+
RandomImpl get random;
118120
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 RandomImpl {
18+
void fillRandomBytes(TypedData destination);
19+
}

lib/src/impl_js/impl_js.dart

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

101101
@override
102102
final sha512 = const _HashImpl('SHA-512');
103+
104+
@override
105+
final random = const _RandomImpl();
103106
}

lib/src/impl_js/impl_js.random.dart

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,22 @@
1414

1515
part of 'impl_js.dart';
1616

17-
void fillRandomBytes(TypedData destination) {
18-
try {
19-
subtle.getRandomValues(destination);
20-
} on subtle.JSDomException catch (e) {
21-
throw _translateDomException(e);
22-
} on Error catch (e) {
23-
final errorName = e.toString();
24-
if (errorName != 'JavaScriptError') {
25-
rethrow;
26-
}
17+
final class _RandomImpl implements RandomImpl {
18+
const _RandomImpl();
19+
20+
@override
21+
void fillRandomBytes(TypedData destination) {
22+
try {
23+
subtle.getRandomValues(destination);
24+
} on subtle.JSDomException catch (e) {
25+
throw _translateDomException(e);
26+
} on Error catch (e) {
27+
final errorName = e.toString();
28+
if (errorName != 'JavaScriptError') {
29+
rethrow;
30+
}
2731

28-
throw _translateJavaScriptException();
32+
throw _translateJavaScriptException();
33+
}
2934
}
3035
}

lib/src/impl_stub.dart

Lines changed: 0 additions & 47 deletions
This file was deleted.

lib/src/impl_stub/impl_stub.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ part 'impl_stub.hkdf.dart';
3030
part 'impl_stub.rsapss.dart';
3131
part 'impl_stub.rsassapkcs1v15.dart';
3232
part 'impl_stub.digest.dart';
33+
part 'impl_stub.random.dart';
3334

3435
const WebCryptoImpl webCryptImpl = _WebCryptoImpl();
3536

@@ -95,4 +96,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {
9596

9697
@override
9798
final sha512 = const _HashImpl();
99+
100+
@override
101+
final random = const _RandomImpl();
98102
}

lib/src/impl_stub/impl_stub.ecdsa.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
part of 'impl_stub.dart';
1616

17-
class _StaticEcdsaPrivateKeyImpl implements StaticEcdsaPrivateKeyImpl {
17+
final class _StaticEcdsaPrivateKeyImpl implements StaticEcdsaPrivateKeyImpl {
1818
const _StaticEcdsaPrivateKeyImpl();
1919

2020
@override
@@ -38,7 +38,7 @@ class _StaticEcdsaPrivateKeyImpl implements StaticEcdsaPrivateKeyImpl {
3838
throw UnimplementedError('Not implemented');
3939
}
4040

41-
class _StaticEcdsaPublicKeyImpl implements StaticEcdsaPublicKeyImpl {
41+
final class _StaticEcdsaPublicKeyImpl implements StaticEcdsaPublicKeyImpl {
4242
const _StaticEcdsaPublicKeyImpl();
4343

4444
@override
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 _RandomImpl implements RandomImpl {
18+
const _RandomImpl();
19+
20+
@override
21+
void fillRandomBytes(TypedData destination) {
22+
throw UnimplementedError('Not implemented');
23+
}
24+
}

0 commit comments

Comments
 (0)