use hmac::{Hmac, Mac};
use sha2::Sha512;
use std::error::Error;
pub fn hmac_sha512(key: impl AsRef<[u8]>, data: impl AsRef<[u8]>) -> Result<Vec<u8>, Box<dyn Error>> {
let mut mac = Hmac::<Sha512>::new_from_slice(key.as_ref())?;
mac.update(data.as_ref());
Ok(mac.finalize().into_bytes().to_vec())
}
#[test]
fn test_hmac_sha512() {
use std::process::Command;
use std::env;
let key = "supersecretkey";
let data = "The quick brown fox jumps over the lazy dog";
let rust_hmac = hmac_sha512(key, data).expect("Failed to compute HMAC-SHA512 in Rust");
let mut ts_script_path = env::current_dir().expect("Failed to get current directory");
ts_script_path.push("src/ts_lib/hmac_sha512.ts");
println!("Using TypeScript script path: {:?}", ts_script_path);
let output = Command::new("npx")
.arg("tsx")
.arg(ts_script_path)
.arg(key)
.arg(data)
.output()
.expect("Failed to execute TypeScript script");
assert!(
output.status.success(),
"TypeScript script failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let ts_hmac_hex = String::from_utf8_lossy(&output.stdout).trim().to_string();
let ts_hmac = hex::decode(&ts_hmac_hex).expect("Invalid hex output from TypeScript script");
assert_eq!(
rust_hmac.as_slice(),
ts_hmac.as_slice(),
"HMAC-SHA512 outputs do not match"
);
}