forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypter.js
86 lines (72 loc) · 2.32 KB
/
decrypter.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* AES128 decryption.
*/
import AES128Decrypter from './aes128-decrypter';
import {ErrorTypes, ErrorDetails} from '../errors';
import {logger} from '../utils/logger';
class Decrypter {
constructor(hls) {
this.hls = hls;
try {
const browserCrypto = window ? window.crypto : crypto;
this.subtle = browserCrypto.subtle || browserCrypto.webkitSubtle;
this.disableWebCrypto = !this.subtle;
} catch (e) {
this.disableWebCrypto = true;
}
}
destroy() {
}
decrypt(data, key, iv, callback) {
if (this.disableWebCrypto && this.hls.config.enableSoftwareAES) {
this.decryptBySoftware(data, key, iv, callback);
} else {
this.decryptByWebCrypto(data, key, iv, callback);
}
}
decryptByWebCrypto(data, key, iv, callback) {
logger.log('decrypting by WebCrypto API');
this.subtle.importKey('raw', key, { name : 'AES-CBC', length : 128 }, false, ['decrypt']).
then((importedKey) => {
this.subtle.decrypt({ name : 'AES-CBC', iv : iv.buffer }, importedKey, data).
then(callback).
catch ((err) => {
this.onWebCryptoError(err, data, key, iv, callback);
});
}).
catch ((err) => {
this.onWebCryptoError(err, data, key, iv, callback);
});
}
decryptBySoftware(data, key8, iv8, callback) {
logger.log('decrypting by JavaScript Implementation');
var view = new DataView(key8.buffer);
var key = new Uint32Array([
view.getUint32(0),
view.getUint32(4),
view.getUint32(8),
view.getUint32(12)
]);
view = new DataView(iv8.buffer);
var iv = new Uint32Array([
view.getUint32(0),
view.getUint32(4),
view.getUint32(8),
view.getUint32(12)
]);
var decrypter = new AES128Decrypter(key, iv);
callback(decrypter.decrypt(data).buffer);
}
onWebCryptoError(err, data, key, iv, callback) {
if (this.hls.config.enableSoftwareAES) {
logger.log('disabling to use WebCrypto API');
this.disableWebCrypto = true;
this.decryptBySoftware(data, key, iv, callback);
}
else {
logger.error(`decrypting error : ${err.message}`);
this.hls.trigger(Event.ERROR, {type : ErrorTypes.MEDIA_ERROR, details : ErrorDetails.FRAG_DECRYPT_ERROR, fatal : true, reason : err.message});
}
}
}
export default Decrypter;