bc_components/x25519/
x25519_public_key.rs1use std::rc::Rc;
2
3use bc_ur::prelude::*;
4
5use crate::{
6 Digest, EncapsulationPublicKey, Encrypter, Error, Reference,
7 ReferenceProvider, Result, tags,
8};
9
10#[derive(Clone, PartialEq, Eq, Hash)]
28pub struct X25519PublicKey([u8; Self::KEY_SIZE]);
29
30impl X25519PublicKey {
31 pub const KEY_SIZE: usize = 32;
32
33 pub const fn from_data(data: [u8; Self::KEY_SIZE]) -> Self { Self(data) }
35
36 pub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> {
38 let data = data.as_ref();
39 if data.len() != Self::KEY_SIZE {
40 return Err(Error::invalid_size(
41 "X25519 public key",
42 Self::KEY_SIZE,
43 data.len(),
44 ));
45 }
46 let mut arr = [0u8; Self::KEY_SIZE];
47 arr.copy_from_slice(data);
48 Ok(Self::from_data(arr))
49 }
50
51 pub fn data(&self) -> &[u8; Self::KEY_SIZE] { self.into() }
53
54 pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
56
57 pub fn from_hex(hex: impl AsRef<str>) -> Self {
64 Self::from_data_ref(hex::decode(hex.as_ref()).unwrap()).unwrap()
65 }
66
67 pub fn hex(&self) -> String { hex::encode(self.data()) }
69}
70
71impl AsRef<[u8]> for X25519PublicKey {
72 fn as_ref(&self) -> &[u8] { &self.0 }
73}
74
75impl From<Rc<X25519PublicKey>> for X25519PublicKey {
78 fn from(value: Rc<X25519PublicKey>) -> Self { value.as_ref().clone() }
79}
80
81impl<'a> From<&'a X25519PublicKey> for &'a [u8; X25519PublicKey::KEY_SIZE] {
84 fn from(value: &'a X25519PublicKey) -> Self { &value.0 }
85}
86
87impl AsRef<X25519PublicKey> for X25519PublicKey {
89 fn as_ref(&self) -> &X25519PublicKey { self }
90}
91
92impl CBORTagged for X25519PublicKey {
94 fn cbor_tags() -> Vec<Tag> {
95 tags_for_values(&[tags::TAG_X25519_PUBLIC_KEY])
96 }
97}
98
99impl From<X25519PublicKey> for CBOR {
101 fn from(value: X25519PublicKey) -> Self { value.tagged_cbor() }
102}
103
104impl CBORTaggedEncodable for X25519PublicKey {
106 fn untagged_cbor(&self) -> CBOR { CBOR::to_byte_string(self.data()) }
107}
108
109impl TryFrom<CBOR> for X25519PublicKey {
112 type Error = dcbor::Error;
113
114 fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
115 Self::from_tagged_cbor(cbor)
116 }
117}
118
119impl CBORTaggedDecodable for X25519PublicKey {
121 fn from_untagged_cbor(untagged_cbor: CBOR) -> dcbor::Result<Self> {
122 let data = CBOR::try_into_byte_string(untagged_cbor)?;
123 Ok(Self::from_data_ref(data)?)
124 }
125}
126
127impl std::fmt::Debug for X25519PublicKey {
129 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130 write!(f, "X25519PublicKey({})", self.hex())
131 }
132}
133
134impl From<&X25519PublicKey> for X25519PublicKey {
137 fn from(key: &X25519PublicKey) -> Self { key.clone() }
138}
139
140impl From<X25519PublicKey> for Vec<u8> {
142 fn from(key: X25519PublicKey) -> Self { key.0.to_vec() }
143}
144
145impl From<&X25519PublicKey> for Vec<u8> {
147 fn from(key: &X25519PublicKey) -> Self { key.0.to_vec() }
148}
149
150impl Encrypter for X25519PublicKey {
152 fn encapsulation_public_key(&self) -> EncapsulationPublicKey {
153 EncapsulationPublicKey::X25519(self.clone())
154 }
155}
156
157impl ReferenceProvider for X25519PublicKey {
159 fn reference(&self) -> Reference {
160 Reference::from_digest(Digest::from_image(
161 self.tagged_cbor().to_cbor_data(),
162 ))
163 }
164}
165
166impl std::fmt::Display for X25519PublicKey {
167 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
168 write!(f, "X25519PublicKey({})", self.ref_hex_short())
169 }
170}