mirror of
https://github.com/RustCrypto/signatures
synced 2026-06-21 13:45:42 +00:00
ed25519: use serdect for serde support (#1324)
Follows suit with `serde` support in other RustCrypto crates by using `serdect` to implement `Serialize` and `Deserialize`, replacing the previous use of `serde_bytes`. These serializers use the `serdect::array` serializers which use efficient format-specific byte encodings (albeit with a length prefix) when serializing to binary formats, or a hex encoding with human readable formats like JSON and TOML.
This commit is contained in:
Generated
+1
-2
@@ -478,8 +478,7 @@ dependencies = [
|
||||
"hex-literal",
|
||||
"pkcs8",
|
||||
"rand_core 0.9.5",
|
||||
"serde",
|
||||
"serde_bytes",
|
||||
"serdect",
|
||||
"signature 3.0.0",
|
||||
"zerocopy",
|
||||
"zeroize",
|
||||
|
||||
+2
-3
@@ -22,8 +22,7 @@ signature = { version = "3", default-features = false }
|
||||
|
||||
# optional dependencies
|
||||
pkcs8 = { version = "0.11", optional = true }
|
||||
serde = { version = "1", optional = true, default-features = false }
|
||||
serde_bytes = { version = "0.11", optional = true, default-features = false }
|
||||
serdect = { version = "0.4", optional = true, default-features = false }
|
||||
zeroize = { version = "1", optional = true, default-features = false }
|
||||
zerocopy = { version = "0.8", optional = true, features = ["derive"] }
|
||||
|
||||
@@ -37,7 +36,7 @@ rand_core = { version = "0.9", features = ["std"] }
|
||||
default = ["alloc"]
|
||||
alloc = ["pkcs8?/alloc"]
|
||||
pem = ["alloc", "pkcs8/pem"]
|
||||
serde_bytes = ["serde", "dep:serde_bytes"]
|
||||
serde = ["dep:serdect"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
+24
-3
@@ -260,9 +260,6 @@ mod hex;
|
||||
#[cfg(feature = "pkcs8")]
|
||||
pub mod pkcs8;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
mod serde;
|
||||
|
||||
pub use signature::{self, Error, SignatureEncoding};
|
||||
|
||||
#[cfg(feature = "pkcs8")]
|
||||
@@ -278,6 +275,8 @@ use core::fmt;
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
use alloc::vec::Vec;
|
||||
#[cfg(feature = "serde")]
|
||||
use serdect::serde::{Deserialize, Serialize, de, ser};
|
||||
|
||||
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
|
||||
use pkcs8::spki::{
|
||||
@@ -451,6 +450,28 @@ impl fmt::Display for Signature {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl Serialize for Signature {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: ser::Serializer,
|
||||
{
|
||||
serdect::array::serialize_hex_upper_or_bin(&self.to_bytes(), serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl<'de> Deserialize<'de> for Signature {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: de::Deserializer<'de>,
|
||||
{
|
||||
let mut bytes = [0u8; Signature::BYTE_SIZE];
|
||||
serdect::array::deserialize_hex_or_bin(&mut bytes, deserializer)?;
|
||||
Ok(bytes.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "zeroize")]
|
||||
impl Zeroize for Signature {
|
||||
fn zeroize(&mut self) {
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
//! `serde` support.
|
||||
|
||||
use crate::{Signature, SignatureBytes};
|
||||
use ::serde::{Deserialize, Serialize, de, ser};
|
||||
use core::fmt;
|
||||
|
||||
impl Serialize for Signature {
|
||||
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
use ser::SerializeTuple;
|
||||
|
||||
let mut seq = serializer.serialize_tuple(Signature::BYTE_SIZE)?;
|
||||
|
||||
for byte in self.to_bytes() {
|
||||
seq.serialize_element(&byte)?;
|
||||
}
|
||||
|
||||
seq.end()
|
||||
}
|
||||
}
|
||||
|
||||
// serde lacks support for deserializing arrays larger than 32-bytes
|
||||
// see: <https://github.com/serde-rs/serde/issues/631>
|
||||
impl<'de> Deserialize<'de> for Signature {
|
||||
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
struct ByteArrayVisitor;
|
||||
|
||||
impl<'de> de::Visitor<'de> for ByteArrayVisitor {
|
||||
type Value = [u8; Signature::BYTE_SIZE];
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter.write_str("bytestring of length 64")
|
||||
}
|
||||
|
||||
fn visit_seq<A>(self, mut seq: A) -> Result<[u8; Signature::BYTE_SIZE], A::Error>
|
||||
where
|
||||
A: de::SeqAccess<'de>,
|
||||
{
|
||||
use de::Error;
|
||||
let mut arr = [0u8; Signature::BYTE_SIZE];
|
||||
|
||||
for (i, byte) in arr.iter_mut().enumerate() {
|
||||
*byte = seq
|
||||
.next_element()?
|
||||
.ok_or_else(|| Error::invalid_length(i, &self))?;
|
||||
}
|
||||
|
||||
Ok(arr)
|
||||
}
|
||||
}
|
||||
|
||||
deserializer
|
||||
.deserialize_tuple(Signature::BYTE_SIZE, ByteArrayVisitor)
|
||||
.map(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde_bytes")]
|
||||
impl serde_bytes::Serialize for Signature {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_bytes(&self.to_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde_bytes")]
|
||||
impl<'de> serde_bytes::Deserialize<'de> for Signature {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: de::Deserializer<'de>,
|
||||
{
|
||||
struct ByteArrayVisitor;
|
||||
|
||||
impl de::Visitor<'_> for ByteArrayVisitor {
|
||||
type Value = SignatureBytes;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter.write_str("bytestring of length 64")
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, bytes: &[u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
use de::Error;
|
||||
|
||||
bytes
|
||||
.try_into()
|
||||
.map_err(|_| Error::invalid_length(bytes.len(), &self))
|
||||
}
|
||||
}
|
||||
|
||||
deserializer
|
||||
.deserialize_bytes(ByteArrayVisitor)
|
||||
.map(Into::into)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user