RSAIdentity: Better Display and Debug.

This commit is contained in:
Nick Mathewson 2020-09-07 17:15:03 -04:00
parent b6a3642903
commit 3df3198363
2 changed files with 15 additions and 1 deletions

View File

@ -27,6 +27,7 @@ sha-1 = "0.9.1"
sha2 = "0.9.1"
sha3 = "0.9.1"
signature = "1.2.2"
hex = "*"
[dev-dependencies]
hex-literal = "0.3.1"

View File

@ -11,6 +11,8 @@
use arrayref::array_ref;
use subtle::*;
use zeroize::Zeroize;
use std::fmt;
use hex;
/// How many bytes are in an "RSA ID"? (This is a legacy tor
/// concept, and refers to identifying a relay by a SHA1 digest
@ -19,7 +21,7 @@ pub const RSA_ID_LEN: usize = 20;
/// An identifier for a Tor relay, based on its legacy RSA
/// identity key. These are used all over the Tor protocol.
#[derive(Clone, Zeroize, Debug)]
#[derive(Clone, Zeroize)]
pub struct RSAIdentity {
id: [u8; RSA_ID_LEN],
}
@ -38,6 +40,17 @@ impl std::hash::Hash for RSAIdentity {
impl Eq for RSAIdentity {}
impl fmt::Display for RSAIdentity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "${}", hex::encode(&self.id[..]))
}
}
impl fmt::Debug for RSAIdentity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "RSAIdentity {{ ${} }}", hex::encode(&self.id[..]))
}
}
impl RSAIdentity {
/// Expose and RSAIdentity as a slice of bytes.
pub fn as_bytes(&self) -> &[u8] {