Skip to content

Commit

Permalink
pkcs1v15::signature::Signature serdect implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LWEdslev committed Mar 19, 2024
1 parent bf61816 commit 6017b0c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
61 changes: 51 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pkcs8 = { version = "=0.11.0-pre.0", default-features = false, features = ["allo
signature = { version = "=2.3.0-pre.3", default-features = false , features = ["alloc", "digest", "rand_core"] }
spki = { version = "=0.8.0-pre.0", default-features = false, features = ["alloc"] }
zeroize = { version = "1.5", features = ["alloc"] }
serdect = "0.2.0"

# optional dependencies
sha1 = { version = "=0.11.0-pre.3", optional = true, default-features = false, features = ["oid"] }
Expand All @@ -33,6 +34,7 @@ serde = { version = "1.0.184", optional = true, default-features = false, featur

[dev-dependencies]
base64ct = { version = "1", features = ["alloc"] }
serde_json = "1.0.114"
hex-literal = "0.4.1"
proptest = "1"
serde_test = "1.0.89"
Expand Down
28 changes: 27 additions & 1 deletion src/pkcs1v15/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::algorithms::pad::uint_to_be_pad;
use ::signature::SignatureEncoding;
use alloc::{boxed::Box, string::ToString};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use serdect::serde::{Deserialize, Serialize};
use core::fmt::{Debug, Display, Formatter, LowerHex, UpperHex};
use num_bigint::BigUint;
use spki::{
Expand Down Expand Up @@ -82,3 +82,29 @@ impl Display for Signature {
write!(f, "{:X}", self)
}
}

#[cfg(feature = "serde")]
impl Serialize for Signature {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: serdect::serde::Serializer,
{
serdect::slice::serialize_hex_lower_or_bin(&self.inner.to_bytes_be(), serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Signature {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: serdect::serde::Deserializer<'de>,
{
let bytes = serdect::slice::deserialize_hex_or_bin_vec(deserializer)?;
let inner = BigUint::from_bytes_be(&bytes);

Ok(Self {
inner,
len: bytes.len(),
})
}
}

0 comments on commit 6017b0c

Please sign in to comment.