Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ipnetwork = { version = "0.21.1", optional = true }
macaddr = { version = "1.0.1", optional = true }
sha1 = { version = "0.11.0", optional = true }
rand = { version = "0.10.2", optional = true }
zeroize = { version = "1.9.0", features = ["zeroize_derive"] }

[dev-dependencies]
expectorate = "1.2.0"
Expand Down
24 changes: 9 additions & 15 deletions src/md5.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
// Copyright 2026 Oxide Computer Company

use std::hash::{Hash, Hasher};
use zeroize::{ZeroizeOnDrop, Zeroizing};

/// An MD5 authentication key represented as a printable ASCII string.
///
/// The key contains between 1 and 80 bytes, inclusive, and every byte is in
/// the printable ASCII range (`0x20..=0x7e`). This follows the recommendation
/// for TCP MD5 keys in RFC 2385 section 4.5.
///
/// The [`Debug`](std::fmt::Debug) implementation redacts the key, and its
/// allocation is zeroized when the value is dropped. Converting it into a
/// [`String`] transfers responsibility for zeroizing that allocation to the
/// caller. Its serialized representation contains the key as a plain string.
#[derive(Clone, Eq, PartialEq, ZeroizeOnDrop)]
pub struct Md5AuthString(Zeroizing<String>);
/// The [`Debug`](std::fmt::Debug) implementation redacts the key, however its
/// serialized representation contains the key as a plain string.
#[derive(Clone, Eq, PartialEq)]
pub struct Md5AuthString(String);

impl Md5AuthString {
/// Maximum key length in bytes.
pub const MAX_LEN: usize = 80;

/// Creates an MD5 authentication string after validating its contents.
pub fn new(source: String) -> Result<Self, Md5AuthStringError> {
let source = Zeroizing::new(source);

if source.is_empty() {
return Err(Md5AuthStringError::Empty);
}
Expand All @@ -49,10 +44,9 @@ impl Md5AuthString {
&self.0
}

/// Returns the underlying string, transferring responsibility for
/// zeroizing it to the caller.
pub fn into_inner(mut self) -> String {
std::mem::take(&mut *self.0)
/// Returns the underlying string.
pub fn into_inner(self) -> String {
self.0
}
}

Expand Down Expand Up @@ -133,8 +127,8 @@ impl schemars::JsonSchema for Md5AuthString {

impl std::error::Error for Md5AuthStringError {}

/// An error returned when an MD5 authentication string violates its required
/// invariants.
/// An error returned when a String fails to meet the required invariants during
/// construction of an Md5AuthSTring.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Md5AuthStringError {
/// The string is empty.
Expand Down
Loading