Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
matrix:
package:
- cryptography
- cryptography_test
- jwk
compiler:
- vm
Expand Down
23 changes: 15 additions & 8 deletions cryptography/lib/src/browser/_javascript_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ Future<ByteBuffer> decrypt(
}

@internal
Future<ByteBuffer> deriveBits(
Future<Uint8List> deriveBits(
JSAny algorithm,
CryptoKey cryptoKey,
JSNumber bits,
) async {
final js = await _deriveBits(algorithm, cryptoKey, bits).toDart;
return js.toDart;
return js.toDart.asUint8List();
}

@internal
Expand Down Expand Up @@ -134,9 +134,9 @@ Future<Jwk> exportKeyWhenJwk(CryptoKey key) async {
}

@internal
Future<ByteBuffer> exportKeyWhenRaw(CryptoKey key) async {
Future<Uint8List> exportKeyWhenRaw(CryptoKey key) async {
final js = await _exportKey('raw'.toJS, key).toDart;
return (js as JSArrayBuffer).toDart;
return (js as JSArrayBuffer).toDart.asUint8List();
}

@internal
Expand Down Expand Up @@ -202,13 +202,13 @@ JSUint8Array jsUint8ListFrom(List<int> data) {
}

@internal
Future<ByteBuffer> sign(
Future<Uint8List> sign(
JSAny algorithm,
CryptoKey key,
JSUint8Array data,
) async {
final js = await _sign(algorithm, key, data).toDart;
return js.toDart;
return js.toDart.asUint8List();
}

@internal
Expand Down Expand Up @@ -330,6 +330,13 @@ extension type AesKeyGenParams._(JSObject jsObject) {
});
}

@internal
extension type AlgorithmNameParams._(JSObject jsObject) {
external factory AlgorithmNameParams({
required JSString name,
});
}

@internal
extension type CryptoKey._(JSObject _) implements JSObject {
external JSObject get algorithm;
Expand All @@ -346,8 +353,8 @@ extension type CryptoKeyPair._(JSObject jsObject) {
}

@internal
extension type EcdhKeyDeriveParams._(JSObject jsObject) {
external factory EcdhKeyDeriveParams({
extension type DeriveParamsWhenPublicKey._(JSObject jsObject) {
external factory DeriveParamsWhenPublicKey({
required JSString name,
required CryptoKey public,
});
Expand Down
3 changes: 1 addition & 2 deletions cryptography/lib/src/browser/browser_secret_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ class BrowserSecretKey extends SecretKey {
return existing;
}
try {
final byteBuffer = await web_crypto.exportKeyWhenRaw(jsCryptoKey);
final bytes = Uint8List.view(byteBuffer);
final bytes = await web_crypto.exportKeyWhenRaw(jsCryptoKey);
final secretKeyData = SecretKeyData(
bytes,
overwriteWhenDestroyed: true,
Expand Down
7 changes: 3 additions & 4 deletions cryptography/lib/src/browser/ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import 'dart:js_interop';
import 'dart:math';
import 'dart:typed_data';

import 'package:cryptography/cryptography.dart';

Expand Down Expand Up @@ -108,15 +107,15 @@ class BrowserEcdh extends Ecdh {
final jsPrivateKey = await jsPrivateKeyFuture;
final jsPublicKey = await jsPublicKeyFuture;
try {
final byteBuffer = await web_crypto.deriveBits(
web_crypto.EcdhKeyDeriveParams(
final derivedBytes = await web_crypto.deriveBits(
web_crypto.DeriveParamsWhenPublicKey(
name: 'ECDH'.toJS,
public: jsPublicKey,
).jsObject,
jsPrivateKey.jsPrivateKeyForEcdh!,
(8 * length).toJS,
);
return SecretKey(Uint8List.view(byteBuffer));
return SecretKey(derivedBytes);
} catch (error, stackTrace) {
throw StateError(
'Web Cryptography throw an error: $error\n$stackTrace',
Expand Down
5 changes: 2 additions & 3 deletions cryptography/lib/src/browser/ecdsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import 'dart:js_interop';
import 'dart:math';
import 'dart:typed_data';

import 'package:cryptography/cryptography.dart';

Expand Down Expand Up @@ -94,7 +93,7 @@ class BrowserEcdsa extends Ecdsa {
allowDeriveBits: true,
);
final jsCryptoKey = browserEcKeyPair.jsPrivateKeyForEcdsa!;
final byteBuffer = await web_crypto.sign(
final signatureBytes = await web_crypto.sign(
web_crypto.EcdsaParams(
name: 'ECDSA'.toJS,
hash: BrowserHashAlgorithmMixin.hashAlgorithmNameFor(
Expand All @@ -106,7 +105,7 @@ class BrowserEcdsa extends Ecdsa {
web_crypto.jsUint8ListFrom(message),
);
return Signature(
Uint8List.view(byteBuffer),
signatureBytes,
publicKey: await publicKeyFuture,
);
}
Expand Down
7 changes: 3 additions & 4 deletions cryptography/lib/src/browser/hkdf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
// limitations under the License.

import 'dart:js_interop';
import 'dart:typed_data';

import 'package:cryptography/cryptography.dart';
import 'package:cryptography/src/browser/hash.dart';

import '_javascript_bindings.dart' show jsUint8ListFrom;
import '_javascript_bindings.dart' as web_crypto;
import '_javascript_bindings.dart' show jsUint8ListFrom;

/// HKDF implementation that uses _Web Cryptography API_ in browsers.
///
Expand Down Expand Up @@ -49,7 +48,7 @@ class BrowserHkdf extends Hkdf {
);
}
final jsCryptoKey = await _jsCryptoKey(secretKey);
final byteBuffer = await web_crypto.deriveBits(
final bytes = await web_crypto.deriveBits(
web_crypto.HkdfParams(
name: 'HKDF'.toJS,
hash: hashAlgorithmName.toJS,
Expand All @@ -59,7 +58,7 @@ class BrowserHkdf extends Hkdf {
jsCryptoKey,
(8 * outputLength).toJS,
);
return SecretKeyData(Uint8List.view(byteBuffer));
return SecretKeyData(bytes);
}

Future<web_crypto.CryptoKey> _jsCryptoKey(SecretKey secretKey) async {
Expand Down
5 changes: 2 additions & 3 deletions cryptography/lib/src/browser/hmac.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

import 'dart:js_interop';
import 'dart:typed_data';

import 'package:cryptography/cryptography.dart';

Expand Down Expand Up @@ -56,12 +55,12 @@ class BrowserHmac extends Hmac {
throw ArgumentError.value(aad, 'aad', 'AAD is unsupported by HMAC');
}
final jsCryptoKey = await _jsCryptoKey(secretKey);
final byteBuffer = await web_crypto.sign(
final macBytes = await web_crypto.sign(
'HMAC'.toJS,
jsCryptoKey,
jsUint8ListFrom(bytes),
);
return Mac(Uint8List.view(byteBuffer));
return Mac(macBytes);
}

Future<web_crypto.CryptoKey> _jsCryptoKey(SecretKey secretKey) async {
Expand Down
5 changes: 2 additions & 3 deletions cryptography/lib/src/browser/pbkdf2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

import 'dart:js_interop';
import 'dart:typed_data';

import 'package:cryptography/cryptography.dart';

Expand Down Expand Up @@ -48,7 +47,7 @@ class BrowserPbkdf2 extends Pbkdf2 {
final jsCryptoKey = await _jsCryptoKey(secretKey);

// subtle.deriveBits(...)
final byteBuffer = await web_crypto.deriveBits(
final derivedBytes = await web_crypto.deriveBits(
web_crypto.Pkdf2Params(
name: 'PBKDF2'.toJS,
hash: macAlgorithm.hashAlgorithmWebCryptoName.toJS,
Expand All @@ -59,7 +58,7 @@ class BrowserPbkdf2 extends Pbkdf2 {
bits.toJS,
);

return SecretKey(Uint8List.view(byteBuffer));
return SecretKey(derivedBytes);
}

Future<web_crypto.CryptoKey> _jsCryptoKey(SecretKey secretKey) async {
Expand Down
4 changes: 2 additions & 2 deletions cryptography/lib/src/browser/rsa_pss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class BrowserRsaPss extends RsaPss {
webCryptoAlgorithm: _webCryptoAlgorithm,
webCryptoHash: webCryptoHash,
);
final byteBuffer = await web_crypto.sign(
final signatureBytes = await web_crypto.sign(
web_crypto.RsaPssParams(
name: _webCryptoAlgorithm.toJS,
saltLength: nonceLengthInBytes.toJS,
Expand All @@ -116,7 +116,7 @@ class BrowserRsaPss extends RsaPss {
Uint8List.fromList(message).toJS,
);
return Signature(
Uint8List.view(byteBuffer),
signatureBytes,
publicKey: await publicKeyFuture,
);
}
Expand Down
5 changes: 2 additions & 3 deletions cryptography/lib/src/browser/rsa_ssa_pkcs1v15.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import 'dart:js_interop';
import 'dart:math';
import 'dart:typed_data';

import 'package:cryptography/cryptography.dart';

Expand Down Expand Up @@ -95,13 +94,13 @@ class BrowserRsaSsaPkcs1v15 extends RsaSsaPkcs1v15 {
webCryptoAlgorithm: _webCryptoAlgorithm,
webCryptoHash: webCryptoHash,
);
final byteBuffer = await web_crypto.sign(
final signatureBytes = await web_crypto.sign(
_webCryptoAlgorithm.toJS,
jsCryptoKey,
web_crypto.jsUint8ListFrom(message),
);
return Signature(
Uint8List.view(byteBuffer),
signatureBytes,
publicKey: await publicKeyFuture,
);
}
Expand Down
Loading
Loading