struct for shared secret without clone or copy
This commit is contained in:
parent
7debf5e213
commit
c81cb03ba7
1 changed files with 12 additions and 3 deletions
15
src/lib.rs
15
src/lib.rs
|
|
@ -48,11 +48,11 @@ impl Secret {
|
|||
Secret::from(U256::random(&mut OsRng))
|
||||
}
|
||||
|
||||
pub fn diffie_hellman(&self, public: &Public) -> Secret {
|
||||
pub fn diffie_hellman(&self, public: &Public) -> SharedSecret {
|
||||
let point = CURVE_25519.point(public.0);
|
||||
let p = point * self.0;
|
||||
|
||||
Secret(p.get_x())
|
||||
SharedSecret(p.get_x().to_le_bytes())
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> [u8; 32] {
|
||||
|
|
@ -72,6 +72,15 @@ impl From<U256> for Secret {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct SharedSecret([u8; 32]);
|
||||
|
||||
impl SharedSecret {
|
||||
pub fn derive_key<T: Fn([u8; 32]) -> [u32; 8]>(self, deriver: T) -> [u32; 8] {
|
||||
deriver(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
@ -103,7 +112,7 @@ mod tests {
|
|||
|
||||
let x_k =
|
||||
U256::from_be_hex("0933dc6ed7122bdf2a5f1b0516e218b743868769778787cfa1ac0ae2dfa89891");
|
||||
assert_eq!(x.0, x_k);
|
||||
assert_eq!(x.0, x_k.to_le_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue