1// Copyright 2015-2021 Brian Smith.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15use super::PublicKey;
16use core::iter::FromIterator;
17
18/// RSA public key components.
19///
20/// `B` must implement `AsRef<[u8]>` like `&[u8]` or `Vec<u8>`.
21#[derive(Clone, Copy)]
22pub struct PublicKeyComponents<B> {
23 /// The public modulus, encoded in big-endian bytes without leading zeros.
24 pub n: B,
25
26 /// The public exponent, encoded in big-endian bytes without leading zeros.
27 pub e: B,
28}
29
30impl<B> core::fmt::Debug for PublicKeyComponents<B>
31where
32 B: core::fmt::Debug,
33{
34 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
35 f&mut DebugStruct<'_, '_>.debug_struct("PublicKeyComponents")
36 .field("n", &self.n)
37 .field(name:"e", &self.e)
38 .finish()
39 }
40}
41
42impl<B> From<&PublicKey> for PublicKeyComponents<B>
43where
44 B: FromIterator<u8>,
45{
46 fn from(public_key: &PublicKey) -> Self {
47 Self {
48 n: public_key.inner().n().be_bytes().collect(),
49 e: public_key.inner().e().be_bytes().collect(),
50 }
51 }
52}
53