Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Jan 31, 2025
1 parent 4acfbe1 commit c4a3b44
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 50 deletions.
3 changes: 1 addition & 2 deletions src/rust/src/backend/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use pyo3::IntoPyObject;

use crate::backend::utils;
use crate::buf::CffiBuf;
use crate::error::{CryptographyError, CryptographyResult};
use crate::exceptions;
Expand Down Expand Up @@ -117,7 +116,7 @@ fn load_pem_private_key<'p>(
))
}
};
let iv = utils::hex_decode(iv).ok_or_else(|| {
let iv = cryptography_crypto::encoding::hex_decode(iv).ok_or_else(|| {
pyo3::exceptions::PyValueError::new_err("DEK-Info IV is not valid hex")
})?;
let key = cryptography_crypto::pbkdf1::openssl_kdf(
Expand Down
48 changes: 0 additions & 48 deletions src/rust/src/backend/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,51 +402,3 @@ pub(crate) fn calculate_digest_and_algorithm<'p>(

Ok((data, algorithm))
}

pub(crate) fn hex_decode(v: &str) -> Option<Vec<u8>> {
if v.len() % 2 != 0 {
return None;
}

let mut b = Vec::with_capacity(v.len() / 2);
let v = v.as_bytes();
for i in (0..v.len()).step_by(2) {
let high = match v[i] {
b @ b'0'..=b'9' => b - b'0',
b @ b'a'..=b'f' => b - b'a' + 10,
b @ b'A'..=b'F' => b - b'A' + 10,
_ => return None,
};

let low = match v[i + 1] {
b @ b'0'..=b'9' => b - b'0',
b @ b'a'..=b'f' => b - b'a' + 10,
b @ b'A'..=b'F' => b - b'A' + 10,
_ => return None,
};

b.push((high << 4) | low);
}

Some(b)
}

#[cfg(test)]
mod tests {
use super::hex_decode;

#[test]
fn test_hex_decode() {
for (text, expected) in [
("", Some(vec![])),
("00", Some(vec![0])),
("0", None),
("12-0", None),
("120-", None),
("ab", Some(vec![0xAB])),
("AB", Some(vec![0xAB])),
] {
assert_eq!(hex_decode(text), expected);
}
}
}

0 comments on commit c4a3b44

Please sign in to comment.