Skip to content

Commit

Permalink
api: decrypt: Validate SHA256 checksum of data
Browse files Browse the repository at this point in the history
Signed-off-by: Shashank Verma <[email protected]>
  • Loading branch information
shank03 committed Jan 26, 2024
1 parent fe19cc3 commit 9550420
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/core/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ impl Cipher {
}
}

/// Generates [`sha::sha256`] hash for the data
pub fn sha256(&self, data: &[u8]) -> String {
hex::encode(sha::sha256(data))
}

/// Encrypts pem using [`XORCryptor`]
pub fn encrypt_pem(&self, key: &Uuid, pem: String) -> Result<String, CipherError> {
let xrc = match XORCryptor::new(&key.to_string()) {
Expand Down Expand Up @@ -155,7 +160,7 @@ impl Cipher {
let time = format!("{}", time);
(
self.encode_string(time.as_bytes()),
hex::encode(sha::sha256(data.as_bytes())),
self.sha256(data.as_bytes()),
)
}
}
5 changes: 5 additions & 0 deletions src/handlers/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ pub async fn decrypt(
Err(err) => return Err(AppError::BadRequest(err.as_str())),
};

let hash = cipher.sha256(data.as_bytes());
if hash != claw.sha256 {
return Err(AppError::BadRequest("SHA256 checksum do not match"));
}

// delete claw and claw_key
match dao::delete_claw(claw.id, &db).await {
Ok(_) => (),
Expand Down

0 comments on commit 9550420

Please sign in to comment.