(()=>{"use strict";let e;var t={},n={};function r(e){var a=n[e];if(void 0!==a)return a.exports;var i=n[e]={exports:{}};return t[e](i,i.exports,r),i.exports}async function a(e){let t=new ArrayBuffer,n=new TextEncoder,r=await crypto.subtle.deriveKey({name:"HKDF",salt:n.encode("TalkFrameEncryptionKey"),hash:"SHA-256",info:t},e,{name:"AES-GCM",length:128},!1,["encrypt","decrypt"]);return{material:e,encryptionKey:r}}async function i(e){let t=new TextEncoder;return crypto.subtle.deriveBits({name:"HKDF",salt:t.encode("TalkFrameRatchetKey"),hash:"SHA-256",info:new ArrayBuffer},e,256)}async function s(e){return crypto.subtle.importKey("raw",e,"HKDF",!1,["deriveBits","deriveKey"])}r.rv=()=>"1.7.5",r.ruid="bundler=rspack@1.7.5";let y={key:10,delta:3,undefined:1};class o{constructor({sharedKey:e=!1}={}){this._cryptoKeyRing=Array(16),this._currentKeyIndex=-1,this._sendCounts=new Map,this._sharedKey=e}async setKey(e,t=-1){let n=!1;if(e)if(this._sharedKey)n=e;else{let t=await s(e);n=await a(t)}this._setKeys(n,t)}_setKeys(e,t=-1){t>=0&&(this._currentKeyIndex=t%this._cryptoKeyRing.length),this._cryptoKeyRing[this._currentKeyIndex]=e,this._sendCount=BigInt(0)}encodeFunction(e,t){let n=this._currentKeyIndex;if(this._cryptoKeyRing[n]){let r=this._makeIV(e.getMetadata().synchronizationSource,e.timestamp),a=new Uint8Array(e.data,0,y[e.type]),i=new Uint8Array(2);return i[0]=12,i[1]=n,crypto.subtle.encrypt({name:"AES-GCM",iv:r,additionalData:new Uint8Array(e.data,0,a.byteLength)},this._cryptoKeyRing[n].encryptionKey,new Uint8Array(e.data,y[e.type])).then(n=>{let s=new ArrayBuffer(a.byteLength+n.byteLength+r.byteLength+i.byteLength),y=new Uint8Array(s);return y.set(a),y.set(new Uint8Array(n),a.byteLength),y.set(new Uint8Array(r),a.byteLength+n.byteLength),y.set(i,a.byteLength+n.byteLength+r.byteLength),e.data=s,t.enqueue(e)},e=>{console.error(e)})}t.enqueue(e)}async decodeFunction(e,t){let n=new Uint8Array(e.data)[e.data.byteLength-1];if(this._cryptoKeyRing[n]){let r=await this._decryptFrame(e,n);r&&t.enqueue(r)}}async _decryptFrame(e,t,n,r=0){let{encryptionKey:o}=this._cryptoKeyRing[t],{material:d}=this._cryptoKeyRing[t];try{let t=new Uint8Array(e.data,0,y[e.type]),n=new Uint8Array(e.data,e.data.byteLength-2,2),r=n[0],a=new Uint8Array(e.data,e.data.byteLength-r-n.byteLength,r),i=t.byteLength,s=e.data.byteLength-(t.byteLength+r+n.byteLength),d=await crypto.subtle.decrypt({name:"AES-GCM",iv:a,additionalData:new Uint8Array(e.data,0,t.byteLength)},o,new Uint8Array(e.data,i,s)),c=new ArrayBuffer(t.byteLength+d.byteLength),h=new Uint8Array(c);return h.set(new Uint8Array(e.data,0,t.byteLength)),h.set(new Uint8Array(d),t.byteLength),e.data=c,e}catch(y){if(this._sharedKey)return;if(r<8){let y=this._cryptoKeyRing[this._currentKeyIndex];d=await s(await i(d));let o=await a(d);return this._setKeys(o),await this._decryptFrame(e,t,n||y,r+1)}this._setKeys(n)}}_makeIV(e,t){let n=new ArrayBuffer(12),r=new DataView(n);this._sendCounts.has(e)||this._sendCounts.set(e,Math.floor(65535*Math.random()));let a=this._sendCounts.get(e);return r.setUint32(0,e),r.setUint32(4,t),r.setUint32(8,a%65535),this._sendCounts.set(e,a+1),n}}let d=new Map;function c(t){return e||(d.has(t)||d.set(t,new o),d.get(t))}function h(e,t,n,r){if("encode"===t||"decode"===t){let a=new TransformStream({transform:("encode"===t?e.encodeFunction:e.decodeFunction).bind(e)});n.pipeThrough(a).pipeTo(r)}else console.error(`Invalid operation: ${t}`)}onmessage=async t=>{let{operation:n}=t.data;if("initialize"===n){let{sharedKey:n}=t.data;n&&(e=new o({sharedKey:n}))}else if("encode"===n||"decode"===n){let{readableStream:e,writableStream:r,participantId:a}=t.data;h(c(a),n,e,r)}else if("setKey"===n){let{participantId:e,key:n,keyIndex:r}=t.data,a=c(e);n?a.setKey(n,r):a.setKey(!1,r)}else if("cleanup"===n){let{participantId:e}=t.data;d.delete(e)}else"cleanupAll"===n?d.clear():console.error("e2ee worker",n)},self.RTCTransformEvent&&(self.onrtctransform=e=>{let t=e.transformer,{operation:n,participantId:r}=t.options;h(c(r),n,t.readable,t.writable)})})(); //# sourceMappingURL=talk-src_utils_e2ee_JitsiEncryptionWorker_worker_js.js.map?v=c5fb7e214b539667