Skip to content

Commit 4c26410

Browse files
committed
Use WebCrypto by default and only use software as a fallback if enabled
Fixes use of `enableSoftwareAES` to match the docs and the intended behavior when added in #99
1 parent 7c0ac43 commit 4c26410

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/crypt/decrypter.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ export default class Decrypter {
1919
private currentIV: ArrayBuffer | null = null;
2020
private currentResult: ArrayBuffer | null = null;
2121
private useSoftware: boolean;
22+
private enableSoftwareAES: boolean;
2223

2324
constructor(config: HlsConfig, { removePKCS7Padding = true } = {}) {
24-
this.useSoftware = config.enableSoftwareAES;
25+
this.enableSoftwareAES = config.enableSoftwareAES;
2526
this.removePKCS7Padding = removePKCS7Padding;
2627
// built in decryptor expects PKCS7 padding
2728
if (removePKCS7Padding) {
@@ -36,9 +37,7 @@ export default class Decrypter {
3637
/* no-op */
3738
}
3839
}
39-
if (this.subtle === null) {
40-
this.useSoftware = true;
41-
}
40+
this.useSoftware = this.subtle === null;
4241
}
4342

4443
destroy() {
@@ -174,14 +173,21 @@ export default class Decrypter {
174173
}
175174

176175
private onWebCryptoError(data, key, iv): ArrayBuffer | never {
177-
this.useSoftware = true;
178-
this.logEnabled = true;
179-
this.softwareDecrypt(data, key, iv);
180-
const decryptResult = this.flush();
181-
if (decryptResult) {
182-
return decryptResult.buffer;
176+
const enableSoftwareAES = this.enableSoftwareAES;
177+
if (enableSoftwareAES) {
178+
this.useSoftware = true;
179+
this.logEnabled = true;
180+
this.softwareDecrypt(data, key, iv);
181+
const decryptResult = this.flush();
182+
if (decryptResult) {
183+
return decryptResult.buffer;
184+
}
183185
}
184-
throw new Error('WebCrypto and softwareDecrypt: failed to decrypt data');
186+
throw new Error(
187+
'WebCrypto' +
188+
(enableSoftwareAES ? ' and softwareDecrypt' : '') +
189+
': failed to decrypt data',
190+
);
185191
}
186192

187193
private getValidChunk(data: Uint8Array): Uint8Array {

0 commit comments

Comments
 (0)