Allow accessing signature::UnparsedPublicKey bytes
Implement `AsRef<[u8]>` for `signature::UnparsedPublicKey`, making it possible to access the bytes of the public key. For consistency, I did the same for `agreement::UnparsedPublicKey`, although it already has a `bytes` method. `agreement::PublicKey` already uses the `AsRef<[u8]>` approach. Also, add missing `Debug` implementation for `signature::UnparsedPublicKey`. I agree to license my contributions to each file under the terms given at the top of each file I changed.
This commit is contained in:
parent
789ba2093b
commit
2afc921340
@ -184,6 +184,15 @@ pub struct UnparsedPublicKey<B> {
|
|||||||
bytes: B,
|
bytes: B,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<B> AsRef<[u8]> for UnparsedPublicKey<B>
|
||||||
|
where
|
||||||
|
B: AsRef<[u8]>,
|
||||||
|
{
|
||||||
|
fn as_ref(&self) -> &[u8] {
|
||||||
|
self.bytes.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<B: core::fmt::Debug> core::fmt::Debug for UnparsedPublicKey<B>
|
impl<B: core::fmt::Debug> core::fmt::Debug for UnparsedPublicKey<B>
|
||||||
where
|
where
|
||||||
B: AsRef<[u8]>,
|
B: AsRef<[u8]>,
|
||||||
@ -202,7 +211,7 @@ impl<B> UnparsedPublicKey<B> {
|
|||||||
Self { algorithm, bytes }
|
Self { algorithm, bytes }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TODO: doc
|
/// The algorithm for the public key.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn algorithm(&self) -> &'static Algorithm {
|
pub fn algorithm(&self) -> &'static Algorithm {
|
||||||
self.algorithm
|
self.algorithm
|
||||||
|
@ -255,7 +255,7 @@
|
|||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use crate::{cpu, ec, error, sealed};
|
use crate::{cpu, debug, ec, error, sealed};
|
||||||
|
|
||||||
pub use crate::ec::{
|
pub use crate::ec::{
|
||||||
curve25519::ed25519::{
|
curve25519::ed25519::{
|
||||||
@ -361,6 +361,27 @@ pub struct UnparsedPublicKey<B> {
|
|||||||
bytes: B,
|
bytes: B,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<B> AsRef<[u8]> for UnparsedPublicKey<B>
|
||||||
|
where
|
||||||
|
B: AsRef<[u8]>,
|
||||||
|
{
|
||||||
|
fn as_ref(&self) -> &[u8] {
|
||||||
|
self.bytes.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B: core::fmt::Debug> core::fmt::Debug for UnparsedPublicKey<B>
|
||||||
|
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<B> UnparsedPublicKey<B> {
|
impl<B> UnparsedPublicKey<B> {
|
||||||
/// Construct a new `UnparsedPublicKey`.
|
/// Construct a new `UnparsedPublicKey`.
|
||||||
///
|
///
|
||||||
|
@ -59,6 +59,9 @@ fn agreement_traits() {
|
|||||||
format!("{:?}", unparsed_public_key),
|
format!("{:?}", unparsed_public_key),
|
||||||
r#"UnparsedPublicKey { algorithm: Algorithm { curve: Curve25519 }, bytes: "010203" }"#
|
r#"UnparsedPublicKey { algorithm: Algorithm { curve: Curve25519 }, bytes: "010203" }"#
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Test `AsRef<[u8]>`
|
||||||
|
assert_eq!(unparsed_public_key.as_ref(), &[0x01, 0x02, 0x03]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -12,4 +12,15 @@ fn signature_impl_test() {
|
|||||||
test::compile_time_assert_copy::<signature::Signature>();
|
test::compile_time_assert_copy::<signature::Signature>();
|
||||||
test::compile_time_assert_send::<signature::Signature>();
|
test::compile_time_assert_send::<signature::Signature>();
|
||||||
test::compile_time_assert_sync::<signature::Signature>();
|
test::compile_time_assert_sync::<signature::Signature>();
|
||||||
|
|
||||||
|
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]);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user