From 6d61da4e32487bdb1f1a84a7b2eddf2df0ddf1ca Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 10 Nov 2020 10:36:50 +0100 Subject: HLS player fallback for non https context --- .../assets/player/p2p-media-loader/segment-validator.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'client/src') diff --git a/client/src/assets/player/p2p-media-loader/segment-validator.ts b/client/src/assets/player/p2p-media-loader/segment-validator.ts index 0614f73d2..fa6e6df1d 100644 --- a/client/src/assets/player/p2p-media-loader/segment-validator.ts +++ b/client/src/assets/player/p2p-media-loader/segment-validator.ts @@ -41,7 +41,7 @@ function segmentValidatorFactory (segmentsSha256Url: string) { throw new Error(`Unknown segment name ${filename}/${range} in segment validator`) } - const calculatedSha = bufferToEx(await sha256(segment.data)) + const calculatedSha = await sha256Hex(segment.data) if (calculatedSha !== hashShouldBe) { throw new Error( `Hashes does not correspond for segment ${filename}/${range}` + @@ -68,14 +68,21 @@ function fetchSha256Segments (url: string) { }) } -function sha256 (data?: ArrayBuffer) { +async function sha256Hex (data?: ArrayBuffer) { if (!data) return undefined - return window.crypto.subtle.digest('SHA-256', data) + if (window.crypto.subtle) { + return window.crypto.subtle.digest('SHA-256', data) + .then(data => bufferToHex(data)) + } + + // Fallback for non HTTPS context + const shaModule = await import('sha.js') + return new shaModule.sha256().update(Buffer.from(data)).digest('hex') } // Thanks: https://stackoverflow.com/a/53307879 -function bufferToEx (buffer?: ArrayBuffer) { +function bufferToHex (buffer?: ArrayBuffer) { if (!buffer) return '' let s = '' -- cgit v1.2.3