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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#![deny(missing_docs)]
use secp256k1::key::SecretKey;
pub use crate::{handshake::handshake_struct::PublicKey, peer_id::PeerId};
pub mod codec;
pub mod crypto;
pub mod error;
mod exchange;
pub mod handshake;
pub mod peer_id;
mod support;
pub type EphemeralPublicKey = Vec<u8>;
#[derive(Clone, Debug)]
pub struct SecioKeyPair {
inner: KeyPairInner,
}
impl SecioKeyPair {
pub fn secp256k1_generated() -> SecioKeyPair {
loop {
if let Ok(private) = SecretKey::from_slice(&rand::random::<
[u8; secp256k1::constants::SECRET_KEY_SIZE],
>()) {
return SecioKeyPair {
inner: KeyPairInner::Secp256k1 { private },
};
}
}
}
pub fn secp256k1_raw_key<K>(key: K) -> Result<SecioKeyPair, error::SecioError>
where
K: AsRef<[u8]>,
{
let private = secp256k1::key::SecretKey::from_slice(key.as_ref())
.map_err(|_| error::SecioError::SecretGenerationFailed)?;
Ok(SecioKeyPair {
inner: KeyPairInner::Secp256k1 { private },
})
}
pub fn public_key(&self) -> PublicKey {
match self.inner {
KeyPairInner::Secp256k1 { ref private } => {
let secp = secp256k1::Secp256k1::signing_only();
let pubkey = secp256k1::key::PublicKey::from_secret_key(&secp, private);
PublicKey::Secp256k1(pubkey.serialize().to_vec())
}
}
}
pub fn peer_id(&self) -> PeerId {
self.public_key().peer_id()
}
}
#[derive(Clone, Debug)]
enum KeyPairInner {
Secp256k1 { private: SecretKey },
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Digest {
Sha256,
Sha512,
}
impl Digest {
#[inline]
pub fn num_bytes(self) -> usize {
match self {
Digest::Sha256 => 256 / 8,
Digest::Sha512 => 512 / 8,
}
}
}