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
101
102
use core::fmt::{self, Debug};
#[cfg(feature = "encoding")]
use subtle_encoding::Encoding;
#[cfg(feature = "encoding")]
use encoding::Decode;
#[cfg(all(feature = "alloc", feature = "encoding"))]
use encoding::Encode;
use error::Error;
#[allow(unused_imports)]
use prelude::*;
use util::fmt_colon_delimited_hex;
pub const PUBLIC_KEY_SIZE: usize = 32;
#[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct PublicKey(pub [u8; PUBLIC_KEY_SIZE]);
impl PublicKey {
pub fn new(bytes: [u8; PUBLIC_KEY_SIZE]) -> Self {
PublicKey(bytes)
}
pub fn from_bytes<B>(bytes: B) -> Result<Self, Error>
where
B: AsRef<[u8]>,
{
ensure!(
bytes.as_ref().len() == PUBLIC_KEY_SIZE,
KeyInvalid,
"expected {}-byte key (got {})",
PUBLIC_KEY_SIZE,
bytes.as_ref().len()
);
let mut public_key = [0u8; PUBLIC_KEY_SIZE];
public_key.copy_from_slice(bytes.as_ref());
Ok(PublicKey(public_key))
}
#[inline]
pub fn as_bytes(&self) -> &[u8; PUBLIC_KEY_SIZE] {
&self.0
}
#[inline]
pub fn into_bytes(self) -> [u8; PUBLIC_KEY_SIZE] {
self.0
}
}
impl AsRef<[u8]> for PublicKey {
#[inline]
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl Debug for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "signatory::ed25519::PublicKey(")?;
fmt_colon_delimited_hex(f, self.as_ref())?;
write!(f, ")")
}
}
#[cfg(feature = "encoding")]
impl Decode for PublicKey {
fn decode<E: Encoding>(encoded_key: &[u8], encoding: &E) -> Result<Self, Error> {
let mut decoded_key = [0u8; PUBLIC_KEY_SIZE];
let decoded_len = encoding.decode_to_slice(encoded_key, &mut decoded_key)?;
ensure!(
decoded_len == PUBLIC_KEY_SIZE,
KeyInvalid,
"invalid {}-byte public key (expected {})",
decoded_len,
PUBLIC_KEY_SIZE
);
Ok(Self::new(decoded_key))
}
}
#[cfg(all(feature = "encoding", feature = "alloc"))]
impl Encode for PublicKey {
fn encode<E: Encoding>(&self, encoding: &E) -> Vec<u8> {
encoding.encode(self.as_bytes())
}
}
impl ::PublicKey for PublicKey {}