Skip to content

Commit

Permalink
refactor symmetric key algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
junkurihara committed Jan 1, 2024
1 parent 38b0268 commit b01cf39
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/crypto/symmetric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ impl SecretKey {
}
}
}
/// Verify the signature
pub fn verify(&self, data: &[u8], signature: &[u8]) -> Result<()> {
match self {
SecretKey::HmacSha256(key) => {
let mut mac = HmacSha256::new_from_slice(&key.key).unwrap();
mac.update(data);
mac.verify(signature.into()).map_err(|e| anyhow::anyhow!(e))?;
Ok(())
}
/// Verify the mac
pub fn verify(&self, data: &[u8], expected_mac: &[u8]) -> Result<()> {
let calcurated_mac = self.sign(data)?;
if calcurated_mac == expected_mac {
Ok(())
} else {
Err(anyhow::anyhow!("Invalid mac"))
}
}

Expand Down

0 comments on commit b01cf39

Please sign in to comment.