Skip to content

Commit 51c520c

Browse files
refactor: update fill-random-bytes
1 parent c178c0b commit 51c520c

File tree

13 files changed

+86
-71
lines changed

13 files changed

+86
-71
lines changed

lib/src/impl_ffi/impl_ffi.dart

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

123123
@override
124124
final sha512 = const _Sha512();
125+
126+
@override
127+
final random = const _RandomImpl();
125128
}

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
@@ -29,6 +29,7 @@ part 'impl_interface.hkdf.dart';
2929
part 'impl_interface.rsapss.dart';
3030
part 'impl_interface.rsassapkcs1v15.dart';
3131
part 'impl_interface.digest.dart';
32+
part 'impl_interface.random.dart';
3233

3334
/// A key-pair as returned from key generation.
3435
class KeyPair<S, T> {
@@ -99,4 +100,5 @@ abstract interface class WebCryptoImpl {
99100
HashImpl get sha256;
100101
HashImpl get sha384;
101102
HashImpl get sha512;
103+
RandomImpl get random;
102104
}
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 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
@@ -109,4 +109,7 @@ final class _WebCryptoImpl implements WebCryptoImpl {
109109

110110
@override
111111
final sha512 = const _HashImpl('SHA-512');
112+
113+
@override
114+
final random = const _RandomImpl();
112115
}

lib/src/impl_js/impl_js.random.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
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);
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+
}
2227
}
2328
}

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)