1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-02 13:39:27 +00:00
hls_m3u8/src/types/encryption_method.rs

81 lines
3 KiB
Rust
Raw Normal View History

2019-09-22 18:33:40 +00:00
use strum::{Display, EnumString};
2019-09-06 11:20:40 +00:00
/// The encryption method.
2020-02-10 12:21:48 +00:00
#[non_exhaustive]
2019-09-06 11:20:40 +00:00
#[allow(missing_docs)]
2019-09-22 18:33:40 +00:00
#[derive(Ord, PartialOrd, Debug, Clone, Copy, PartialEq, Eq, Hash, Display, EnumString)]
#[strum(serialize_all = "SCREAMING-KEBAB-CASE")]
2019-09-06 11:20:40 +00:00
pub enum EncryptionMethod {
/// The [`MediaSegment`]s are completely encrypted using the Advanced
/// Encryption Standard ([AES-128]) with a 128-bit key, Cipher Block
/// Chaining (CBC), and [Public-Key Cryptography Standards #7 (PKCS7)]
/// padding.
2020-02-02 12:38:11 +00:00
///
2019-09-15 14:45:43 +00:00
/// CBC is restarted on each segment boundary, using either the
/// Initialization Vector (IV) or the Media Sequence Number as the IV
///
/// ```
/// # let media_sequence_number = 5;
/// # assert_eq!(
/// format!("0x{:032x}", media_sequence_number)
2020-02-24 15:45:10 +00:00
/// # , "0x00000000000000000000000000000005".to_string());
/// ```
2019-09-15 14:45:43 +00:00
///
2020-02-10 12:21:48 +00:00
/// [`MediaSegment`]: crate::MediaSegment
/// [AES-128]: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf
2019-09-15 14:45:43 +00:00
/// [Public-Key Cryptography Standards #7 (PKCS7)]: https://tools.ietf.org/html/rfc5652
2019-09-22 18:33:40 +00:00
#[strum(serialize = "AES-128")]
2019-09-06 11:20:40 +00:00
Aes128,
/// The [`MediaSegment`]s contain media samples, such as audio or video,
/// that are encrypted using the Advanced Encryption Standard ([`AES-128`]).
///
/// How these media streams are encrypted and encapsulated in a segment
/// depends on the media encoding and the media format of the segment.
///
/// `fMP4` [`MediaSegment`]s are encrypted using the `cbcs` scheme of
/// [Common Encryption].
/// Encryption of other [`MediaSegment`] formats containing [H.264], [AAC],
/// [AC-3], and Enhanced [AC-3] media streams is described in the
/// [HTTP Live Streaming (HLS) SampleEncryption specification].
2019-09-15 14:45:43 +00:00
///
2020-02-02 12:38:11 +00:00
/// [`MediaSegment`]: crate::MediaSegment
2020-02-10 12:21:48 +00:00
/// [`AES-128`]: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf
2019-09-15 14:45:43 +00:00
/// [Common Encryption]: https://tools.ietf.org/html/rfc8216#ref-COMMON_ENC
/// [H.264]: https://tools.ietf.org/html/rfc8216#ref-H_264
/// [AAC]: https://tools.ietf.org/html/rfc8216#ref-ISO_14496
/// [AC-3]: https://tools.ietf.org/html/rfc8216#ref-AC_3
/// [HTTP Live Streaming (HLS) SampleEncryption specification]:
/// https://tools.ietf.org/html/rfc8216#ref-SampleEnc
2019-09-06 11:20:40 +00:00
SampleAes,
}
2019-09-06 11:46:21 +00:00
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
2019-09-06 11:46:21 +00:00
#[test]
fn test_display() {
2019-09-15 14:45:43 +00:00
assert_eq!(EncryptionMethod::Aes128.to_string(), "AES-128".to_string());
assert_eq!(
EncryptionMethod::SampleAes.to_string(),
"SAMPLE-AES".to_string()
);
2019-09-06 11:46:21 +00:00
}
#[test]
2019-09-21 11:24:05 +00:00
fn test_parser() {
2019-09-06 11:46:21 +00:00
assert_eq!(
2019-09-15 14:45:43 +00:00
EncryptionMethod::Aes128,
2019-09-06 11:46:21 +00:00
"AES-128".parse::<EncryptionMethod>().unwrap()
);
assert_eq!(
2019-09-15 14:45:43 +00:00
EncryptionMethod::SampleAes,
2019-09-06 11:46:21 +00:00
"SAMPLE-AES".parse::<EncryptionMethod>().unwrap()
);
2019-09-15 14:45:43 +00:00
2019-09-22 18:33:40 +00:00
assert!("unknown".parse::<EncryptionMethod>().is_err());
2019-09-06 11:46:21 +00:00
}
}