Skip to content

Commit

Permalink
don't try to use local crypto if the API is unavailable
Browse files Browse the repository at this point in the history
this also enabled usage in chrome web workers by accessing the crypto object directly
  • Loading branch information
kanongil committed Dec 4, 2015
1 parent cd5132c commit 7008b0c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"globals" : {
"console" : true,
"MediaSource" : true,
"performance" : true
"performance" : true,
"crypto" : true
}
}
23 changes: 14 additions & 9 deletions src/crypt/decrypter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ class Decrypter {

constructor(hls) {
this.hls = hls;
this.disableWebCrypto = false;
try {
const browserCrypto = window ? window.crypto : crypto;
this.subtle = browserCrypto.subtle;
this.disableWebCrypto = !this.subtle;
} catch (e) {
this.disableWebCrypto = true;
}
}

destroy() {
Expand All @@ -27,17 +33,16 @@ class Decrypter {
decryptByWebCrypto(data, key, iv, callback) {
logger.log('decrypting by WebCrypto API');

var localthis = this;
window.crypto.subtle.importKey('raw', key, { name : 'AES-CBC', length : 128 }, false, ['decrypt']).
then(function (importedKey) {
window.crypto.subtle.decrypt({ name : 'AES-CBC', iv : iv.buffer }, importedKey, data).
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 (function (err) {
localthis.onWebCryptoError(err, data, key, iv, callback);
catch ((err) => {
this.onWebCryptoError(err, data, key, iv, callback);
});
}).
catch (function (err) {
localthis.onWebCryptoError(err, data, key, iv, callback);
catch ((err) => {
this.onWebCryptoError(err, data, key, iv, callback);
});
}

Expand Down

0 comments on commit 7008b0c

Please sign in to comment.