summaryrefslogtreecommitdiffstats
path: root/third_party/rust/ohttp/src/rh/mod.rs
blob: 8f91a2ab17274f529247a38a90cb9c9f5313df45 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub mod aead;
pub mod hkdf;
pub mod hpke;

use crate::err::Res;

pub struct SymKey(Vec<u8>);

impl SymKey {
    #[allow(clippy::unnecessary_wraps)]
    pub fn key_data(&self) -> Res<&[u8]> {
        Ok(&self.0)
    }
}

impl From<Vec<u8>> for SymKey {
    fn from(v: Vec<u8>) -> Self {
        SymKey(v)
    }
}
impl From<&[u8]> for SymKey {
    fn from(v: &[u8]) -> Self {
        SymKey(v.to_owned())
    }
}

impl AsRef<[u8]> for SymKey {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl std::fmt::Debug for SymKey {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if let Ok(b) = self.key_data() {
            write!(f, "SymKey {}", hex::encode(b))
        } else {
            write!(f, "Opaque SymKey")
        }
    }
}