diff --git a/src/agreement.rs b/src/agreement.rs index e195b9ee8..17a158da4 100644 --- a/src/agreement.rs +++ b/src/agreement.rs @@ -184,6 +184,15 @@ pub struct UnparsedPublicKey { bytes: B, } +impl AsRef<[u8]> for UnparsedPublicKey +where + B: AsRef<[u8]>, +{ + fn as_ref(&self) -> &[u8] { + self.bytes.as_ref() + } +} + impl core::fmt::Debug for UnparsedPublicKey where B: AsRef<[u8]>, @@ -202,7 +211,7 @@ impl UnparsedPublicKey { Self { algorithm, bytes } } - /// TODO: doc + /// The algorithm for the public key. #[inline] pub fn algorithm(&self) -> &'static Algorithm { self.algorithm diff --git a/src/signature.rs b/src/signature.rs index 64f7d0893..b1f910e03 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -255,7 +255,7 @@ //! # } //! ``` -use crate::{cpu, ec, error, sealed}; +use crate::{cpu, debug, ec, error, sealed}; pub use crate::ec::{ curve25519::ed25519::{ @@ -361,6 +361,27 @@ pub struct UnparsedPublicKey { bytes: B, } +impl AsRef<[u8]> for UnparsedPublicKey +where + B: AsRef<[u8]>, +{ + fn as_ref(&self) -> &[u8] { + self.bytes.as_ref() + } +} + +impl core::fmt::Debug for UnparsedPublicKey +where + B: AsRef<[u8]>, +{ + fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> { + f.debug_struct("UnparsedPublicKey") + .field("algorithm", &self.algorithm) + .field("bytes", &debug::HexStr(self.bytes.as_ref())) + .finish() + } +} + impl UnparsedPublicKey { /// Construct a new `UnparsedPublicKey`. /// diff --git a/tests/agreement_tests.rs b/tests/agreement_tests.rs index edafdb1f3..648bbcac6 100644 --- a/tests/agreement_tests.rs +++ b/tests/agreement_tests.rs @@ -59,6 +59,9 @@ fn agreement_traits() { format!("{:?}", unparsed_public_key), r#"UnparsedPublicKey { algorithm: Algorithm { curve: Curve25519 }, bytes: "010203" }"# ); + + // Test `AsRef<[u8]>` + assert_eq!(unparsed_public_key.as_ref(), &[0x01, 0x02, 0x03]); } #[test] diff --git a/tests/signature_tests.rs b/tests/signature_tests.rs index 383eaee3b..b3a048eea 100644 --- a/tests/signature_tests.rs +++ b/tests/signature_tests.rs @@ -12,4 +12,15 @@ fn signature_impl_test() { test::compile_time_assert_copy::(); test::compile_time_assert_send::(); test::compile_time_assert_sync::(); + + let unparsed_public_key = + signature::UnparsedPublicKey::new(&signature::ED25519, &[0x01, 0x02, 0x03]); + + assert_eq!( + format!("{:?}", unparsed_public_key), + r#"UnparsedPublicKey { algorithm: ring::signature::ED25519, bytes: "010203" }"# + ); + + // Test `AsRef<[u8]>` + assert_eq!(unparsed_public_key.as_ref(), &[0x01, 0x02, 0x03]); }