prople_crypto/
types.rs

1use rst_common::standard::bytes::Bytes;
2use rst_common::with_cryptography::blake3::{self, Hash};
3use rst_common::with_cryptography::ed25519_dalek::VerifyingKey;
4use rst_common::with_cryptography::hex;
5use rst_common::with_cryptography::x25519_dalek::{PublicKey as ECDHPublicKey, SharedSecret};
6
7use super::errors::CommonError;
8
9/// `Hexer` is a trait used for any value or data types that possible to encode it's
10/// original value to the hex encoded format
11pub trait Hexer {
12    fn hex(&self) -> String;
13}
14
15pub trait Value<T> {
16    fn get(&self) -> Result<T, CommonError>;
17}
18
19pub trait StringValue {
20    fn get_string(&self) -> String;
21}
22
23/// `BytesValue` is a trait used to get common bytes array
24/// The return value will be wrapped in [`Bytes`] container object to simplify
25/// the bytes arrary process
26pub trait BytesValue {
27    fn bytes(&self) -> Bytes;
28}
29
30/// `VectorValue` is a trait used to get main vector value. It has a generic parameter used to
31/// indicate a real data types will used inside the vector
32pub trait VectorValue<T> {
33    fn vec(&self) -> Vec<T>;
34}
35
36/// `ByteHex` is a new type that wrap the [`String`] which should be an output of encoded hex format
37/// This *newtype* will able to generated from the [`SharedSecret`] and [`ECDHPublicKey`], and if there is
38/// a common string value it also possible to generate from it
39#[derive(PartialEq, Debug, Clone)]
40pub struct ByteHex(String);
41
42impl Hexer for ByteHex {
43    fn hex(&self) -> String {
44        self.0.to_owned()
45    }
46}
47
48impl From<String> for ByteHex {
49    fn from(value: String) -> Self {
50        ByteHex(value)
51    }
52}
53
54impl From<SharedSecret> for ByteHex {
55    fn from(value: SharedSecret) -> Self {
56        ByteHex::from(hex::encode(value.to_bytes()))
57    }
58}
59
60impl From<ECDHPublicKey> for ByteHex {
61    fn from(value: ECDHPublicKey) -> Self {
62        ByteHex::from(hex::encode(value.to_bytes()))
63    }
64}
65
66impl From<VerifyingKey> for ByteHex {
67    fn from(value: VerifyingKey) -> Self {
68        ByteHex::from(hex::encode(value.to_bytes()))
69    }
70}
71
72/// `Blake3Hash` is a *newtype* used to wrap given [`blake3::Hash`] value type
73/// This *newtype* will implement [`Hexer`] which means it's possible to get a hex encoded
74/// format from the hash bytes value. It also possible to generate from vector of byte which
75/// will automatically hash the given bytes array into hash
76#[derive(PartialEq, Debug, Clone)]
77pub struct Blake3Hash(Hash);
78
79impl BytesValue for Blake3Hash {
80    fn bytes(&self) -> Bytes {
81        Bytes::from(self.0.as_bytes().to_vec())
82    }
83}
84
85impl Hexer for Blake3Hash {
86    fn hex(&self) -> String {
87        hex::encode(self.0.as_bytes())
88    }
89}
90
91impl From<Vec<u8>> for Blake3Hash {
92    fn from(value: Vec<u8>) -> Self {
93        let hashed = blake3::hash(value.as_slice());
94        Blake3Hash(hashed)
95    }
96}