1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::{InternalError, SecretKey};
use arrayref::array_ref;
use failure::Error;
use std::convert::TryFrom;
pub const PUBLIC_KEY_LENGTH: usize = 64;
#[derive(Clone, Debug)]
pub struct PublicKey {
pub ed: ed25519_dalek::PublicKey,
pub dh: x25519_dalek::PublicKey,
}
impl From<&SecretKey> for PublicKey {
fn from(secret_key: &SecretKey) -> Self {
Self {
ed: ed25519_dalek::PublicKey::from(&secret_key.ed),
dh: x25519_dalek::PublicKey::from(&secret_key.dh),
}
}
}
impl TryFrom<&[u8]> for PublicKey {
type Error = Error;
fn try_from(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() != PUBLIC_KEY_LENGTH {
Err(InternalError::BytesLengthError.into())
} else {
Ok(Self {
ed: ed25519_dalek::PublicKey::from_bytes(&bytes[32..])?,
dh: x25519_dalek::PublicKey::from(*array_ref!(bytes, 0, 32)),
})
}
}
}
impl Into<[u8; PUBLIC_KEY_LENGTH]> for &PublicKey {
fn into(self) -> [u8; PUBLIC_KEY_LENGTH] {
let mut buf = [0u8; PUBLIC_KEY_LENGTH];
buf[..32].copy_from_slice(self.dh.as_bytes());
buf[32..].copy_from_slice(self.ed.as_bytes());
buf
}
}