forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes-crypto.ts
32 lines (29 loc) · 956 Bytes
/
aes-crypto.ts
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
import { DecrypterAesMode } from './decrypter-aes-mode';
export default class AESCrypto {
private subtle: SubtleCrypto;
private aesIV: Uint8Array;
private aesMode: DecrypterAesMode;
constructor(subtle: SubtleCrypto, iv: Uint8Array, aesMode: DecrypterAesMode) {
this.subtle = subtle;
this.aesIV = iv;
this.aesMode = aesMode;
}
decrypt(data: ArrayBuffer, key: CryptoKey) {
switch (this.aesMode) {
case DecrypterAesMode.cbc:
return this.subtle.decrypt(
{ name: 'AES-CBC', iv: this.aesIV },
key,
data,
);
case DecrypterAesMode.ctr:
return this.subtle.decrypt(
{ name: 'AES-CTR', counter: this.aesIV, length: 64 }, //64 : NIST SP800-38A standard suggests that the counter should occupy half of the counter block
key,
data,
);
default:
throw new Error(`[AESCrypto] invalid aes mode ${this.aesMode}`);
}
}
}