Skip to content

Commit 88a344a

Browse files
cursoragentlovasoa
andcommitted
Refactor hmac function for cleaner output formatting
Co-authored-by: contact <[email protected]>
1 parent d9544c6 commit 88a344a

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -757,14 +757,14 @@ async fn hmac<'a>(
757757
};
758758

759759
let algorithm = algorithm.as_deref().unwrap_or("sha256");
760-
760+
761761
// Parse algorithm and output format (e.g., "sha256" or "sha256-base64")
762762
let (hash_algo, output_format) = if let Some((algo, format)) = algorithm.split_once('-') {
763763
(algo, format)
764764
} else {
765765
(algorithm, "hex")
766766
};
767-
767+
768768
let result = match hash_algo.to_lowercase().as_str() {
769769
"sha256" => {
770770
let mut mac = Hmac::<Sha256>::new_from_slice(key.as_bytes())
@@ -787,15 +787,11 @@ async fn hmac<'a>(
787787

788788
// Convert to requested output format
789789
let output = match output_format.to_lowercase().as_str() {
790-
"hex" => {
791-
result.into_iter().fold(String::new(), |mut acc, byte| {
792-
write!(&mut acc, "{byte:02x}").unwrap();
793-
acc
794-
})
795-
}
796-
"base64" => {
797-
base64::Engine::encode(&base64::engine::general_purpose::STANDARD, result)
798-
}
790+
"hex" => result.into_iter().fold(String::new(), |mut acc, byte| {
791+
write!(&mut acc, "{byte:02x}").unwrap();
792+
acc
793+
}),
794+
"base64" => base64::Engine::encode(&base64::engine::general_purpose::STANDARD, result),
799795
_ => {
800796
anyhow::bail!(
801797
"Unsupported output format: {output_format}. Supported formats: hex, base64"

0 commit comments

Comments
 (0)