This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7ea51d
commit 5239ef0
Showing
26 changed files
with
2,420 additions
and
2,518 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,45 @@ | ||
use derive_more::{AsMut, AsRef}; | ||
use crate::PetError; | ||
|
||
use crate::{crypto::ByteObject, PetError}; | ||
|
||
#[derive(AsRef, AsMut, Clone, Debug, PartialEq, Eq)] | ||
#[derive(Debug, PartialEq)] | ||
/// A dummy certificate. | ||
pub struct Certificate(Vec<u8>); | ||
|
||
impl ByteObject for Certificate { | ||
/// Create a certificate a slice of bytes. Fails if the length of the input is invalid. | ||
fn from_slice(bytes: &[u8]) -> Option<Self> { | ||
Some(Self(bytes.to_vec())) | ||
#[allow(clippy::len_without_is_empty)] | ||
impl Certificate { | ||
#[allow(clippy::new_without_default)] | ||
/// Create a certificate | ||
pub fn new() -> Self { | ||
Self(vec![0_u8; 32]) | ||
} | ||
|
||
/// Create a certificate initialized to zero. | ||
fn zeroed() -> Self { | ||
Self(vec![0_u8; Self::BYTES]) | ||
/// Get the length of the certificate. | ||
pub fn len(&self) -> usize { | ||
self.as_ref().len() | ||
} | ||
|
||
/// Get the certificate as a slice. | ||
fn as_slice(&self) -> &[u8] { | ||
self.0.as_slice() | ||
/// Validate a certificate | ||
pub fn validate(&self) -> Result<(), PetError> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[allow(clippy::len_without_is_empty)] | ||
impl Certificate { | ||
/// Get the number of bytes of a certificate. | ||
pub const BYTES: usize = 32; | ||
|
||
/// Get the length of the serialized certificate. | ||
pub fn len(&self) -> usize { | ||
self.as_slice().len() | ||
} | ||
|
||
/// Serialize the certificate into bytes. | ||
pub fn serialize(&self) -> Vec<u8> { | ||
self.as_slice().to_vec() | ||
impl AsRef<[u8]> for Certificate { | ||
/// Get a reference to the certificate. | ||
fn as_ref(&self) -> &[u8] { | ||
self.0.as_slice() | ||
} | ||
} | ||
|
||
/// Deserialize the certificate from bytes. Fails if the length of the input is invalid. | ||
pub fn deserialize(bytes: &[u8]) -> Result<Self, PetError> { | ||
Self::from_slice(bytes).ok_or(PetError::InvalidMessage) | ||
impl From<Vec<u8>> for Certificate { | ||
/// Create a certificate from bytes. | ||
fn from(bytes: Vec<u8>) -> Self { | ||
Self(bytes) | ||
} | ||
} | ||
|
||
/// Validate the certificate. | ||
pub fn validate(&self) -> Result<(), PetError> { | ||
if self.as_slice() == [0_u8; 32] { | ||
Ok(()) | ||
} else { | ||
Err(PetError::InvalidMessage) | ||
} | ||
impl From<&[u8]> for Certificate { | ||
/// Create a certificate from a slice of bytes. | ||
fn from(slice: &[u8]) -> Self { | ||
Self(slice.to_vec()) | ||
} | ||
} |
Oops, something went wrong.