use super::Algorithm;
use crate::error::*;
#[allow(unreachable_pub)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct ECPublicKey {
buf: [u8; MAX_LEN],
len: usize,
pub(crate) algorithm: Algorithm,
}
const MAX_LEN: usize = 1 + (2 * 48);
#[allow(unreachable_pub)]
impl ECPublicKey {
pub fn from_unprefixed(without_prefix: &[u8], algorithm: Algorithm) -> ProtoResult<Self> {
let field_len = match algorithm {
Algorithm::ECDSAP256SHA256 => 32,
Algorithm::ECDSAP384SHA384 => 48,
_ => return Err("only ECDSAP256SHA256 and ECDSAP384SHA384 are supported by Ec".into()),
};
let len = 1 + (2 * field_len);
if len - 1 != without_prefix.len() {
return Err("EC public key is the wrong length".into());
}
let mut buf = [0x04u8; MAX_LEN];
buf[1..len].copy_from_slice(without_prefix);
Ok(Self {
buf,
len,
algorithm,
})
}
pub fn prefixed_bytes(&self) -> &[u8] {
&self.buf[..self.len]
}
pub fn unprefixed_bytes(&self) -> &[u8] {
&self.buf[1..self.len]
}
}