1use cipher::{BlockModeDecrypt, KeyIvInit};
2
3type Aes128Cbc = cbc::Decryptor<aes::Aes128>;
4
5#[derive(Clone)]
10pub struct HlsAes128Decrypter {
11 key: [u8; 16],
12 iv: [u8; 16],
13}
14
15impl HlsAes128Decrypter {
16 pub fn new(key: &[u8; 16], iv: &[u8; 16]) -> Self {
18 Self { key: *key, iv: *iv }
19 }
20
21 pub fn increment_iv(&mut self) {
23 self.iv = (u128::from_be_bytes(self.iv) + 1).to_be_bytes();
24 }
25
26 pub fn decrypt(&self, mut input: Vec<u8>) -> Vec<u8> {
28 let slice_len = {
29 let slice = Aes128Cbc::new((&self.key).into(), (&self.iv).into())
30 .decrypt_padded::<cipher::block_padding::Pkcs7>(&mut input)
31 .unwrap();
32 slice.len()
33 };
34
35 input.truncate(slice_len);
36 input
37 }
38}
39
40#[derive(Clone)]
45pub struct HlsSampleAesDecrypter {
46 key: [u8; 16],
47 iv: [u8; 16],
48}
49
50impl HlsSampleAesDecrypter {
51 pub fn new(key: &[u8; 16], iv: &[u8; 16]) -> Self {
53 Self { key: *key, iv: *iv }
54 }
55
56 pub fn increment_iv(&mut self) {
58 self.iv = (u128::from_be_bytes(self.iv) + 1).to_be_bytes();
59 }
60
61 pub fn decrypt(&self, input: Vec<u8>) -> Vec<u8> {
63 let mut input = std::io::Cursor::new(input);
64 let mut output = Vec::new();
65 iori_ssa::decrypt(&mut input, &mut output, self.key, self.iv).unwrap();
66 output
67 }
68}