Preshared keys
This commit is contained in:
parent
bb6d657ac8
commit
bc2078fe24
1 changed files with 47 additions and 0 deletions
47
src/key.rs
47
src/key.rs
|
@ -15,6 +15,10 @@ pub struct PubKey {
|
|||
data: [u8; 32],
|
||||
}
|
||||
|
||||
pub struct PresharedKey {
|
||||
data: [u8; 32],
|
||||
}
|
||||
|
||||
impl Key for PrivKey {
|
||||
fn get_data(&self) -> [u8; 32] {
|
||||
self.data.to_owned()
|
||||
|
@ -27,6 +31,12 @@ impl Key for PubKey {
|
|||
}
|
||||
}
|
||||
|
||||
impl Key for PresharedKey {
|
||||
fn get_data(&self) -> [u8; 32] {
|
||||
self.data.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for dyn Key {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
|
@ -54,6 +64,7 @@ impl Display for PrivKey {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for PubKey {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
|
@ -68,6 +79,20 @@ impl Display for PubKey {
|
|||
}
|
||||
}
|
||||
|
||||
impl Display for PresharedKey {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
base64::engine::GeneralPurpose::new(
|
||||
&base64::alphabet::STANDARD,
|
||||
base64::engine::general_purpose::GeneralPurposeConfig::new()
|
||||
)
|
||||
.encode(self.get_data())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl PrivKey {
|
||||
pub fn generate() -> Self {
|
||||
let mut buf: [u8; 32] = [0; 32];
|
||||
|
@ -87,6 +112,14 @@ impl PrivKey {
|
|||
}
|
||||
}
|
||||
|
||||
impl PresharedKey {
|
||||
pub fn generate() -> Self {
|
||||
let mut buf: [u8; 32] = [0; 32];
|
||||
StdRng::from_entropy().fill_bytes(&mut buf);
|
||||
PresharedKey { data: buf }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<[u8; 32]> for PrivKey {
|
||||
fn from(value: [u8; 32]) -> Self {
|
||||
PrivKey { data: value }
|
||||
|
@ -101,6 +134,20 @@ impl From<&[u8; 32]> for PrivKey {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<[u8; 32]> for PresharedKey {
|
||||
fn from(value: [u8; 32]) -> Self {
|
||||
PresharedKey { data: value }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&[u8; 32]> for PresharedKey {
|
||||
fn from(value: &[u8; 32]) -> Self {
|
||||
PresharedKey {
|
||||
data: value.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_key_pair() -> (PrivKey, PubKey) {
|
||||
let privkey = PrivKey::generate();
|
||||
let pubkey = privkey.derive_pubkey();
|
||||
|
|
Loading…
Reference in a new issue