Шифрование тест 3
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import _sodium from "libsodium-wrappers";
|
||||
import _sodium from "libsodium-wrappers-sumo";
|
||||
|
||||
type KeyInput = Buffer | Uint8Array;
|
||||
|
||||
@@ -17,64 +17,48 @@ export async function initE2EE(): Promise<void> {
|
||||
|
||||
function toUint8Array(input: KeyInput): Uint8Array {
|
||||
const u8 = input instanceof Uint8Array ? input : new Uint8Array(input);
|
||||
return new Uint8Array(u8.slice().buffer);
|
||||
return new Uint8Array(u8.buffer.slice(u8.byteOffset, u8.byteOffset + u8.byteLength));
|
||||
}
|
||||
|
||||
function fillNonceFromTimestamp(nonce: Uint8Array, tsRaw: unknown): void {
|
||||
nonce.fill(0);
|
||||
|
||||
let ts = 0n;
|
||||
if (typeof tsRaw === "bigint") ts = tsRaw;
|
||||
else if (typeof tsRaw === "number" && Number.isFinite(tsRaw)) ts = BigInt(Math.floor(tsRaw));
|
||||
|
||||
// Записываем 64-bit timestamp в первые 8 байт nonce (BE)
|
||||
nonce[0] = Number((ts >> 56n) & 0xffn);
|
||||
nonce[1] = Number((ts >> 48n) & 0xffn);
|
||||
nonce[2] = Number((ts >> 40n) & 0xffn);
|
||||
nonce[3] = Number((ts >> 32n) & 0xffn);
|
||||
nonce[4] = Number((ts >> 24n) & 0xffn);
|
||||
nonce[5] = Number((ts >> 16n) & 0xffn);
|
||||
nonce[6] = Number((ts >> 8n) & 0xffn);
|
||||
nonce[7] = Number(ts & 0xffn);
|
||||
}
|
||||
|
||||
function createFrameProcessor(key: Uint8Array) {
|
||||
// Выделяем nonce один раз — переиспользуем на каждый фрейм
|
||||
const nonce = new Uint8Array(sodium.crypto_stream_chacha20_NONCEBYTES); // 8 bytes
|
||||
const nonceView = new DataView(nonce.buffer, 0, nonce.byteLength);
|
||||
const nonceLen = sodium.crypto_stream_xchacha20_NONCEBYTES; // 24
|
||||
const nonce = new Uint8Array(nonceLen);
|
||||
|
||||
return function processFrame(data: ArrayBuffer, timestamp: number): ArrayBuffer {
|
||||
return function processFrame(data: ArrayBuffer, timestamp: unknown): ArrayBuffer {
|
||||
const input = new Uint8Array(data);
|
||||
fillNonceFromTimestamp(nonce, timestamp);
|
||||
|
||||
// Безопасно записываем nonce через отдельный DataView
|
||||
nonceView.setUint32(0, (timestamp >>> 0) & 0xffffffff, false);
|
||||
nonceView.setUint32(4, ((timestamp / 0x100000000) >>> 0) & 0xffffffff, false);
|
||||
|
||||
const output = sodium.crypto_stream_chacha20_xor(input, nonce, key);
|
||||
return output.buffer as ArrayBuffer;
|
||||
const output = sodium.crypto_stream_xchacha20_xor(input, nonce, key);
|
||||
return output.buffer.slice(output.byteOffset, output.byteOffset + output.byteLength) as ArrayBuffer;
|
||||
};
|
||||
}
|
||||
|
||||
function createTransform(processFrame: (data: ArrayBuffer, timestamp: number) => ArrayBuffer) {
|
||||
let frames = 0;
|
||||
let slowFrames = 0;
|
||||
let total = 0;
|
||||
let max = 0;
|
||||
|
||||
function createTransform(processFrame: (data: ArrayBuffer, timestamp: unknown) => ArrayBuffer) {
|
||||
return new TransformStream<any, any>({
|
||||
transform(frame, controller) {
|
||||
const started = performance.now();
|
||||
|
||||
try {
|
||||
// Передаём timestamp фрейма как nonce
|
||||
const ts = typeof frame.timestamp === "number" ? frame.timestamp : 0;
|
||||
frame.data = processFrame(frame.data, ts);
|
||||
frame.data = processFrame(frame.data, frame.timestamp);
|
||||
} catch (e) {
|
||||
console.error("[E2EE] frame error:", e);
|
||||
controller.enqueue(frame);
|
||||
return;
|
||||
}
|
||||
|
||||
const elapsed = performance.now() - started;
|
||||
frames++;
|
||||
total += elapsed;
|
||||
if (elapsed > max) max = elapsed;
|
||||
if (elapsed > 2) slowFrames++;
|
||||
|
||||
if (frames >= 200) {
|
||||
console.info("[E2EE stats]", {
|
||||
avgMs: +(total / frames).toFixed(3),
|
||||
maxMs: +max.toFixed(3),
|
||||
slowFrames,
|
||||
});
|
||||
frames = 0;
|
||||
slowFrames = 0;
|
||||
total = 0;
|
||||
max = 0;
|
||||
}
|
||||
|
||||
controller.enqueue(frame);
|
||||
}
|
||||
});
|
||||
@@ -87,8 +71,9 @@ export async function attachSenderE2EE(sender: RTCRtpSender, keyInput: KeyInput)
|
||||
await initE2EE();
|
||||
|
||||
const key = toUint8Array(keyInput);
|
||||
if (key.byteLength < sodium.crypto_stream_chacha20_KEYBYTES) {
|
||||
throw new Error(`Key must be at least ${sodium.crypto_stream_chacha20_KEYBYTES} bytes`);
|
||||
const keyLen = sodium.crypto_stream_xchacha20_KEYBYTES; // 32
|
||||
if (key.byteLength < keyLen) {
|
||||
throw new Error(`Key must be at least ${keyLen} bytes`);
|
||||
}
|
||||
|
||||
const anySender = sender as any;
|
||||
@@ -97,7 +82,7 @@ export async function attachSenderE2EE(sender: RTCRtpSender, keyInput: KeyInput)
|
||||
}
|
||||
|
||||
const { readable, writable } = anySender.createEncodedStreams();
|
||||
const processFrame = createFrameProcessor(key.slice(0, sodium.crypto_stream_chacha20_KEYBYTES));
|
||||
const processFrame = createFrameProcessor(key.slice(0, keyLen));
|
||||
|
||||
readable
|
||||
.pipeThrough(createTransform(processFrame))
|
||||
@@ -112,8 +97,9 @@ export async function attachReceiverE2EE(receiver: RTCRtpReceiver, keyInput: Key
|
||||
await initE2EE();
|
||||
|
||||
const key = toUint8Array(keyInput);
|
||||
if (key.byteLength < sodium.crypto_stream_chacha20_KEYBYTES) {
|
||||
throw new Error(`Key must be at least ${sodium.crypto_stream_chacha20_KEYBYTES} bytes`);
|
||||
const keyLen = sodium.crypto_stream_xchacha20_KEYBYTES; // 32
|
||||
if (key.byteLength < keyLen) {
|
||||
throw new Error(`Key must be at least ${keyLen} bytes`);
|
||||
}
|
||||
|
||||
const anyReceiver = receiver as any;
|
||||
@@ -122,7 +108,7 @@ export async function attachReceiverE2EE(receiver: RTCRtpReceiver, keyInput: Key
|
||||
}
|
||||
|
||||
const { readable, writable } = anyReceiver.createEncodedStreams();
|
||||
const processFrame = createFrameProcessor(key.slice(0, sodium.crypto_stream_chacha20_KEYBYTES));
|
||||
const processFrame = createFrameProcessor(key.slice(0, keyLen));
|
||||
|
||||
readable
|
||||
.pipeThrough(createTransform(processFrame))
|
||||
|
||||
Reference in New Issue
Block a user