bc_components/x25519/
x25519_public_key.rs1use std::rc::Rc;
2
3use bc_ur::prelude::*;
4
5use crate::{EncapsulationPublicKey, Encrypter, Error, Result, tags};
6
7#[derive(Clone, PartialEq, Eq, Hash)]
25pub struct X25519PublicKey([u8; Self::KEY_SIZE]);
26
27impl X25519PublicKey {
28 pub const KEY_SIZE: usize = 32;
29
30 pub const fn from_data(data: [u8; Self::KEY_SIZE]) -> Self { Self(data) }
32
33 pub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> {
35 let data = data.as_ref();
36 if data.len() != Self::KEY_SIZE {
37 return Err(Error::invalid_size(
38 "X25519 public key",
39 Self::KEY_SIZE,
40 data.len(),
41 ));
42 }
43 let mut arr = [0u8; Self::KEY_SIZE];
44 arr.copy_from_slice(data);
45 Ok(Self::from_data(arr))
46 }
47
48 pub fn data(&self) -> &[u8; Self::KEY_SIZE] { self.into() }
50
51 pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
53
54 pub fn from_hex(hex: impl AsRef<str>) -> Self {
61 Self::from_data_ref(hex::decode(hex.as_ref()).unwrap()).unwrap()
62 }
63
64 pub fn hex(&self) -> String { hex::encode(self.data()) }
66}
67
68impl AsRef<[u8]> for X25519PublicKey {
69 fn as_ref(&self) -> &[u8] { &self.0 }
70}
71
72impl From<Rc<X25519PublicKey>> for X25519PublicKey {
75 fn from(value: Rc<X25519PublicKey>) -> Self { value.as_ref().clone() }
76}
77
78impl<'a> From<&'a X25519PublicKey> for &'a [u8; X25519PublicKey::KEY_SIZE] {
81 fn from(value: &'a X25519PublicKey) -> Self { &value.0 }
82}
83
84impl AsRef<X25519PublicKey> for X25519PublicKey {
86 fn as_ref(&self) -> &X25519PublicKey { self }
87}
88
89impl CBORTagged for X25519PublicKey {
91 fn cbor_tags() -> Vec<Tag> {
92 tags_for_values(&[tags::TAG_X25519_PUBLIC_KEY])
93 }
94}
95
96impl From<X25519PublicKey> for CBOR {
98 fn from(value: X25519PublicKey) -> Self { value.tagged_cbor() }
99}
100
101impl CBORTaggedEncodable for X25519PublicKey {
103 fn untagged_cbor(&self) -> CBOR { CBOR::to_byte_string(self.data()) }
104}
105
106impl TryFrom<CBOR> for X25519PublicKey {
109 type Error = dcbor::Error;
110
111 fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
112 Self::from_tagged_cbor(cbor)
113 }
114}
115
116impl CBORTaggedDecodable for X25519PublicKey {
118 fn from_untagged_cbor(untagged_cbor: CBOR) -> dcbor::Result<Self> {
119 let data = CBOR::try_into_byte_string(untagged_cbor)?;
120 Ok(Self::from_data_ref(data)?)
121 }
122}
123
124impl std::fmt::Debug for X25519PublicKey {
126 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127 write!(f, "X25519PublicKey({})", self.hex())
128 }
129}
130
131impl From<&X25519PublicKey> for X25519PublicKey {
134 fn from(key: &X25519PublicKey) -> Self { key.clone() }
135}
136
137impl From<X25519PublicKey> for Vec<u8> {
139 fn from(key: X25519PublicKey) -> Self { key.0.to_vec() }
140}
141
142impl From<&X25519PublicKey> for Vec<u8> {
144 fn from(key: &X25519PublicKey) -> Self { key.0.to_vec() }
145}
146
147impl Encrypter for X25519PublicKey {
149 fn encapsulation_public_key(&self) -> EncapsulationPublicKey {
150 EncapsulationPublicKey::X25519(self.clone())
151 }
152}