use hmac::{Hmac, KeyInit, Mac};
use sha2::Sha256;
use subtle::ConstantTimeEq;
use crate::error::RazorpayError;
type HmacSha256 = Hmac<Sha256>;
pub fn compute_signature(message: &str, secret: &str) -> String {
let mut mac = HmacSha256::new_from_slice(secret.as_bytes())
.expect("HMAC accepts keys of any length");
mac.update(message.as_bytes());
hex::encode(mac.finalize().into_bytes())
}
#[must_use = "an ignored verification result is the same as no verification at all"]
pub fn verify_signature(message: &str, signature: &str, secret: &str) -> Result<(), RazorpayError> {
let mut mac = HmacSha256::new_from_slice(secret.as_bytes())
.expect("HMAC accepts keys of any length");
mac.update(message.as_bytes());
let expected = mac.finalize().into_bytes();
let provided = match hex::decode(signature) {
Ok(bytes) => bytes,
Err(_) => return Err(RazorpayError::SignatureMismatch),
};
if provided.len() != expected.len() {
return Err(RazorpayError::SignatureMismatch);
}
if provided.ct_eq(&expected).into() {
Ok(())
} else {
Err(RazorpayError::SignatureMismatch)
}
}
#[must_use = "an ignored verification result is the same as no verification at all"]
pub fn verify_payment_signature(
order_id: &str,
payment_id: &str,
signature: &str,
key_secret: &str,
) -> Result<(), RazorpayError> {
verify_signature(&format!("{order_id}|{payment_id}"), signature, key_secret)
}
#[must_use = "an ignored verification result is the same as no verification at all"]
pub fn verify_webhook_signature(
payload: &str,
signature: &str,
webhook_secret: &str,
) -> Result<(), RazorpayError> {
verify_signature(payload, signature, webhook_secret)
}
#[cfg(test)]
mod tests {
use super::*;
const HELLO_SECRET: &str = "88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0b";
#[test]
fn compute_matches_known_vector() {
assert_eq!(compute_signature("hello", "secret"), HELLO_SECRET);
}
#[test]
fn verify_accepts_correct_signature() {
assert!(verify_signature("hello", HELLO_SECRET, "secret").is_ok());
}
#[test]
fn verify_rejects_wrong_secret() {
assert!(matches!(
verify_signature("hello", HELLO_SECRET, "wrong"),
Err(RazorpayError::SignatureMismatch)
));
}
#[test]
fn verify_rejects_tampered_message() {
assert!(verify_signature("hello!", HELLO_SECRET, "secret").is_err());
}
#[test]
fn verify_rejects_non_hex_signature() {
assert!(verify_signature("hello", "zzzz", "secret").is_err());
}
#[test]
fn verify_rejects_truncated_signature() {
assert!(verify_signature("hello", &HELLO_SECRET[..32], "secret").is_err());
}
#[test]
fn verify_is_case_insensitive_on_hex() {
assert!(verify_signature("hello", &HELLO_SECRET.to_uppercase(), "secret").is_ok());
}
#[test]
fn payment_signature_uses_pipe_separated_message() {
let expected = compute_signature("order_1|pay_1", "sec");
assert!(verify_payment_signature("order_1", "pay_1", &expected, "sec").is_ok());
assert!(verify_payment_signature("order_1|pay", "_1", &expected, "sec").is_err());
}
#[test]
fn webhook_signature_is_over_exact_bytes() {
let body = r#"{"event":"payment.captured"}"#;
let sig = compute_signature(body, "whsec");
assert!(verify_webhook_signature(body, &sig, "whsec").is_ok());
assert!(verify_webhook_signature(r#"{"event": "payment.captured"}"#, &sig, "whsec").is_err());
}
}