Browse Source

protocol: Do not print admin password hash

ipv6
Denis Drakhnia 1 year ago
parent
commit
35b4abbf15
  1. 10
      protocol/src/admin.rs
  2. 24
      protocol/src/types.rs

10
protocol/src/admin.rs

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2023 Denis Drakhnia <numas13@gmail.com>
use crate::cursor::{Cursor, CursorMut};
use crate::types::Str;
use crate::types::{Hide, Str};
use crate::Error;
pub const HASH_LEN: usize = 64;
@ -30,7 +30,7 @@ impl AdminChallenge { @@ -30,7 +30,7 @@ impl AdminChallenge {
#[derive(Clone, Debug, PartialEq)]
pub struct AdminCommand<'a> {
pub hash: &'a [u8],
pub hash: Hide<&'a [u8]>,
pub command: Str<&'a [u8]>,
}
@ -39,7 +39,7 @@ impl<'a> AdminCommand<'a> { @@ -39,7 +39,7 @@ impl<'a> AdminCommand<'a> {
pub fn new(hash: &'a [u8], command: &'a str) -> Self {
Self {
hash,
hash: Hide(hash),
command: Str(command.as_bytes()),
}
}
@ -47,7 +47,7 @@ impl<'a> AdminCommand<'a> { @@ -47,7 +47,7 @@ impl<'a> AdminCommand<'a> {
pub fn decode_with_hash_len(hash_len: usize, src: &'a [u8]) -> Result<Self, Error> {
let mut cur = Cursor::new(src);
cur.expect(Self::HEADER)?;
let hash = cur.get_bytes(hash_len)?;
let hash = Hide(cur.get_bytes(hash_len)?);
let command = Str(cur.get_bytes(cur.remaining())?);
cur.expect_empty()?;
Ok(Self { hash, command })
@ -61,7 +61,7 @@ impl<'a> AdminCommand<'a> { @@ -61,7 +61,7 @@ impl<'a> AdminCommand<'a> {
pub fn encode(&self, buf: &mut [u8]) -> Result<usize, Error> {
Ok(CursorMut::new(buf)
.put_bytes(Self::HEADER)?
.put_bytes(self.hash)?
.put_bytes(&self.hash)?
.put_bytes(&self.command)?
.pos())
}

24
protocol/src/types.rs

@ -49,3 +49,27 @@ impl<T> Deref for Str<T> { @@ -49,3 +49,27 @@ impl<T> Deref for Str<T> {
&self.0
}
}
/// Wrapper for slice of bytes without printing
#[derive(Copy, Clone, PartialEq, Eq, Default)]
pub struct Hide<T>(pub T);
impl<T> fmt::Debug for Hide<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "<hidden>")
}
}
impl<T> fmt::Display for Hide<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "<hidden>")
}
}
impl<T> Deref for Hide<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}

Loading…
Cancel
Save