use serde::{Deserialize, Serialize};
use crate::{error::MalformedError, keyring::KEY_ID_LEN};
use super::{Algorithm, Segment};
#[derive(Debug, Clone, Serialize, Deserialize, Copy, PartialEq, Eq)]
#[serde(try_from = "u8", into = "u8")]
pub enum Method {
Online,
StreamingHmacSha256(Segment),
}
impl Method {
pub fn is_online(self) -> bool {
matches!(self, Method::Online)
}
pub fn is_stream(self) -> bool {
matches!(self, Method::StreamingHmacSha256(_))
}
pub fn header_len(&self, algorithm: Algorithm) -> usize {
match self {
Method::Online => Method::LEN + KEY_ID_LEN + algorithm.nonce_len(),
Method::StreamingHmacSha256(_) => {
Method::LEN + KEY_ID_LEN + algorithm.key_len() + algorithm.nonce_prefix_len()
}
}
}
pub fn to_be_bytes(self) -> [u8; 1] {
[u8::from(self)]
}
pub(super) const LEN: usize = 1;
}
impl From<Method> for u8 {
fn from(method: Method) -> Self {
match method {
Method::Online => 0,
Method::StreamingHmacSha256(segment) => match segment {
Segment::FourKilobytes => 1,
Segment::SixtyFourKilobytes => 2,
Segment::OneMegabyte => 3,
Segment::FourMegabytes => 4,
},
}
}
}
impl PartialEq<u8> for Method {
fn eq(&self, other: &u8) -> bool {
u8::from(*self) == *other
}
}
impl PartialEq<Method> for u8 {
fn eq(&self, other: &Method) -> bool {
*self == u8::from(*other)
}
}
impl From<Method> for usize {
fn from(method: Method) -> Self {
u8::from(method) as usize
}
}
impl TryFrom<u8> for Method {
type Error = MalformedError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Method::Online),
1 => Ok(Method::StreamingHmacSha256(Segment::FourKilobytes)),
2 => Ok(Method::StreamingHmacSha256(Segment::SixtyFourKilobytes)),
3 => Ok(Method::StreamingHmacSha256(Segment::OneMegabyte)),
4 => Ok(Method::StreamingHmacSha256(Segment::FourMegabytes)),
_ => Err("missing or unknown encryption method".into()),
}
}
}