|
| 1 | +use azure_core::headers::{self, AsHeaders, HeaderName, HeaderValue}; |
| 2 | + |
| 3 | +const DEFAULT_ENCRYPTION_ALGORITHM: &str = "AES256"; |
| 4 | + |
| 5 | +#[derive(Clone, Debug)] |
| 6 | +pub struct CPKInfo { |
| 7 | + encryption_key: String, |
| 8 | + encryption_key_sha256: String, |
| 9 | + |
| 10 | + // only support AES256 |
| 11 | + encryption_algorithm: Option<String>, |
| 12 | +} |
| 13 | + |
| 14 | +impl CPKInfo { |
| 15 | + pub fn new(key: String, key_sha256: String, algorithm: Option<String>) -> Self { |
| 16 | + Self { |
| 17 | + encryption_key: key, |
| 18 | + encryption_key_sha256: key_sha256, |
| 19 | + |
| 20 | + encryption_algorithm: algorithm, |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl From<(String, String)> for CPKInfo { |
| 26 | + fn from(s: (String, String)) -> Self { |
| 27 | + Self::new(s.0, s.1, None) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl From<(String, String, String)> for CPKInfo { |
| 32 | + fn from(s: (String, String, String)) -> Self { |
| 33 | + Self::new(s.0, s.1, Some(s.2)) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl AsHeaders for CPKInfo { |
| 38 | + type Iter = std::vec::IntoIter<(HeaderName, HeaderValue)>; |
| 39 | + |
| 40 | + fn as_headers(&self) -> Self::Iter { |
| 41 | + let algorithm = self |
| 42 | + .encryption_algorithm |
| 43 | + .as_deref() |
| 44 | + .unwrap_or(DEFAULT_ENCRYPTION_ALGORITHM) |
| 45 | + .to_owned(); |
| 46 | + let headers = vec![ |
| 47 | + (headers::ENCRYPTION_ALGORITHM, algorithm.into()), |
| 48 | + ( |
| 49 | + headers::ENCRYPTION_KEY, |
| 50 | + self.encryption_key.to_owned().into(), |
| 51 | + ), |
| 52 | + ( |
| 53 | + headers::ENCRYPTION_KEY_SHA256, |
| 54 | + self.encryption_key_sha256.to_owned().into(), |
| 55 | + ), |
| 56 | + ]; |
| 57 | + headers.into_iter() |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl AsHeaders for &CPKInfo { |
| 62 | + type Iter = <CPKInfo as AsHeaders>::Iter; |
| 63 | + |
| 64 | + fn as_headers(&self) -> Self::Iter { |
| 65 | + (*self).as_headers() |
| 66 | + } |
| 67 | +} |
0 commit comments