MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / random_mac_key

Function random_mac_key

atomic-agent/src/turn/session.rs:714–741  ·  view source on GitHub ↗

32 bytes of evidence-quality randomness for the session MAC key. Prefers OS randomness; falls back to a process/time mix so evidence capture still works in sandboxes without `/dev/urandom`. This is not a secret: the key is stored beside the evidence it protects.

()

Source from the content-addressed store, hash-verified

712/// capture still works in sandboxes without `/dev/urandom`. This is not a
713/// secret: the key is stored beside the evidence it protects.
714fn random_mac_key() -> String {
715 let mut bytes = [0u8; 32];
716 #[cfg(unix)]
717 {
718 use std::io::Read;
719 if let Ok(mut file) = std::fs::File::open("/dev/urandom") {
720 if file.read_exact(&mut bytes).is_ok() {
721 return hex_encode(&bytes);
722 }
723 }
724 }
725 // Deterministic fallback: unique per session/process/creation instant.
726 let nanos = std::time::SystemTime::now()
727 .duration_since(std::time::UNIX_EPOCH)
728 .map(|d| d.as_nanos())
729 .unwrap_or(0);
730 let mix = format!(
731 "{}:{}:{}",
732 nanos,
733 std::process::id(),
734 "atomic-agent-mac-key"
735 );
736 bytes.copy_from_slice(&blake3::derive_key(
737 "atomic-agent session mac key",
738 mix.as_bytes(),
739 ));
740 hex_encode(&bytes)
741}
742
743/// Lowercase hex encoding for MAC keys.
744fn hex_encode(bytes: &[u8]) -> String {

Callers 1

ensure_mac_keyMethod · 0.85

Calls 4

openFunction · 0.85
hex_encodeFunction · 0.70
read_exactMethod · 0.45
as_bytesMethod · 0.45

Tested by

no test coverage detected