From ed0f17dea764fea93ea7c2b897fa4a9e31c9476e Mon Sep 17 00:00:00 2001 From: diogo464 Date: Tue, 17 Feb 2026 11:41:02 +0000 Subject: add const key macro and internal base64 codec --- src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 8ba36eb..72fb58f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,35 @@ pub use view::*; pub use ipnet; +#[doc(hidden)] +pub const fn __decode_wg_key_const( + encoded: &str, +) -> [u8; netlink_packet_wireguard::WireguardAttribute::WG_KEY_LEN] { + key::decode_wg_key_const(encoded) +} + +/// Creates a [`Key`] from a canonical WireGuard base64 literal at compile time. +/// +/// The literal must be exactly 44 characters of standard base64 and end with `=`. +/// +/// ``` +/// use wireguard::{key, Key}; +/// +/// const PRIVATE_KEY: Key = key!("6F5rOtYE5A2KcXTKf9jdzWa9Y/kuV5gPS3LcKlxmOnY="); +/// ``` +/// +/// ```compile_fail +/// use wireguard::key; +/// +/// const _BAD: wireguard::Key = key!("not-a-wireguard-key"); +/// ``` +#[macro_export] +macro_rules! key { + ($value:literal) => { + $crate::Key::new_unchecked_from($crate::__decode_wg_key_const($value)) + }; +} + pub type Result = std::result::Result; #[derive(Debug)] -- cgit