11import 'dart:async' ;
2- import 'dart:html ' ;
2+ import 'dart:js_interop ' ;
33import 'dart:js_util' as jsutil;
44import 'dart:typed_data' ;
55
6- import 'crypto.dart' as crypto;
6+ import 'package:web/web.dart' as web;
7+
78import 'e2ee.logger.dart' ;
89import 'e2ee.utils.dart' ;
910
11+ final crypto = web.window.crypto.subtle;
12+
1013class KeyOptions {
1114 KeyOptions ({
1215 required this .sharedKey,
@@ -29,7 +32,7 @@ class KeyOptions {
2932
3033class KeyProvider {
3134 KeyProvider (this .worker, this .id, this .keyProviderOptions);
32- final DedicatedWorkerGlobalScope worker;
35+ final web. DedicatedWorkerGlobalScope worker;
3336 final String id;
3437 final KeyOptions keyProviderOptions;
3538 var participantKeys = < String , ParticipantKeyHandler > {};
@@ -80,8 +83,8 @@ const KEYRING_SIZE = 16;
8083
8184class KeySet {
8285 KeySet (this .material, this .encryptionKey);
83- CryptoKey material;
84- CryptoKey encryptionKey;
86+ web. CryptoKey material;
87+ web. CryptoKey encryptionKey;
8588}
8689
8790class ParticipantKeyHandler {
@@ -100,7 +103,7 @@ class ParticipantKeyHandler {
100103
101104 final KeyOptions keyOptions;
102105
103- final DedicatedWorkerGlobalScope worker;
106+ final web. DedicatedWorkerGlobalScope worker;
104107
105108 final String participantIdentity;
106109
@@ -150,22 +153,23 @@ class ParticipantKeyHandler {
150153 return null ;
151154 }
152155 var newKey = await ratchet (currentMaterial, keyOptions.ratchetSalt);
153- var newMaterial = await ratchetMaterial (
154- currentMaterial, crypto.jsArrayBufferFrom (newKey));
156+ var newMaterial = await ratchetMaterial (currentMaterial, newKey.buffer);
155157 var newKeySet = await deriveKeys (newMaterial, keyOptions.ratchetSalt);
156158 await setKeySetFromMaterial (newKeySet, keyIndex ?? currentKeyIndex);
157159 return newKey;
158160 }
159161
160- Future <CryptoKey > ratchetMaterial (
161- CryptoKey currentMaterial, ByteBuffer newKeyBuffer) async {
162- var newMaterial = await jsutil.promiseToFuture (crypto.importKey (
163- 'raw' ,
164- newKeyBuffer,
165- (currentMaterial.algorithm as crypto.Algorithm ).name,
166- false ,
167- ['deriveBits' , 'deriveKey' ],
168- ));
162+ Future <web.CryptoKey > ratchetMaterial (
163+ web.CryptoKey currentMaterial, ByteBuffer newKeyBuffer) async {
164+ var newMaterial = await crypto
165+ .importKey (
166+ 'raw' ,
167+ newKeyBuffer.toJS,
168+ (currentMaterial.algorithm as web.Algorithm ).name.toJS,
169+ false ,
170+ < JSString > ['deriveBits' .toJS, 'deriveKey' .toJS].toJS,
171+ )
172+ .toDart;
169173 return newMaterial;
170174 }
171175
@@ -174,8 +178,11 @@ class ParticipantKeyHandler {
174178 }
175179
176180 Future <void > setKey (Uint8List key, {int keyIndex = 0 }) async {
177- var keyMaterial = await crypto.impportKeyFromRawData (key,
178- webCryptoAlgorithm: 'PBKDF2' , keyUsages: ['deriveBits' , 'deriveKey' ]);
181+ var keyMaterial = await crypto
182+ .importKey ('raw' , key.toJS, 'PBKDF2' .toJS, false ,
183+ < JSString > ['deriveBits' .toJS, 'deriveKey' .toJS].toJS)
184+ .toDart;
185+
179186 var keySet = await deriveKeys (
180187 keyMaterial,
181188 keyOptions.ratchetSalt,
@@ -194,19 +201,19 @@ class ParticipantKeyHandler {
194201
195202 /// Derives a set of keys from the master key.
196203 /// See https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.1
197- Future <KeySet > deriveKeys (CryptoKey material, Uint8List salt) async {
204+ Future <KeySet > deriveKeys (web. CryptoKey material, Uint8List salt) async {
198205 var algorithmOptions =
199- getAlgoOptions ((material.algorithm as crypto .Algorithm ).name, salt);
206+ getAlgoOptions ((material.algorithm as web .Algorithm ).name, salt);
200207
201208 // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#HKDF
202209 // https://developer.mozilla.org/en-US/docs/Web/API/HkdfParams
203210 var encryptionKey =
204- await jsutil.promiseToFuture <CryptoKey >(crypto.deriveKey (
211+ await jsutil.promiseToFuture< web. CryptoKey > (crypto.deriveKey (
205212 jsutil.jsify (algorithmOptions),
206213 material,
207214 jsutil.jsify ({'name' : 'AES-GCM' , 'length' : 128 }),
208215 false ,
209- ['encrypt' , 'decrypt' ] ,
216+ < JSString > ['encrypt' .toJS , 'decrypt' .toJS].toJS ,
210217 ));
211218
212219 return KeySet (material, encryptionKey);
@@ -215,7 +222,7 @@ class ParticipantKeyHandler {
215222 /// Ratchets a key. See
216223 /// https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.5.1
217224
218- Future <Uint8List > ratchet (CryptoKey material, Uint8List salt) async {
225+ Future <Uint8List > ratchet (web. CryptoKey material, Uint8List salt) async {
219226 var algorithmOptions = getAlgoOptions ('PBKDF2' , salt);
220227
221228 // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits
0 commit comments