blob: 8f4922daa040f0ec6e1ec3bc01eba5c8229c3e97 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import { Segment } from 'p2p-media-loader-core'
import { basename } from 'path'
function segmentValidatorFactory (segmentsSha256Url: string) {
const segmentsJSON = fetchSha256Segments(segmentsSha256Url)
return async function segmentValidator (segment: Segment) {
const segmentName = basename(segment.url)
const hashShouldBe = (await segmentsJSON)[segmentName]
if (hashShouldBe === undefined) {
throw new Error(`Unknown segment name ${segmentName} in segment validator`)
}
const calculatedSha = bufferToEx(await sha256(segment.data))
if (calculatedSha !== hashShouldBe) {
throw new Error(`Hashes does not correspond for segment ${segmentName} (expected: ${hashShouldBe} instead of ${calculatedSha})`)
}
}
}
// ---------------------------------------------------------------------------
export {
segmentValidatorFactory
}
// ---------------------------------------------------------------------------
function fetchSha256Segments (url: string) {
return fetch(url)
.then(res => res.json())
.catch(err => {
console.error('Cannot get sha256 segments', err)
return {}
})
}
function sha256 (data?: ArrayBuffer) {
if (!data) return undefined
return window.crypto.subtle.digest('SHA-256', data)
}
// Thanks: https://stackoverflow.com/a/53307879
function bufferToEx (buffer?: ArrayBuffer) {
if (!buffer) return ''
let s = ''
const h = '0123456789abcdef'
const o = new Uint8Array(buffer)
o.forEach((v: any) => s += h[ v >> 4 ] + h[ v & 15 ])
return s
}
|