Hey guys, there is a problem I am facing with OpenSSL crate,
This is my test function:
fn custom_body_test() {
let body = include_str!("Mail.eml"); //Email we want to calculate sha256
let canonized: String = relaxed_canonicalize_body(body);
let file = std::fs::File::create("Mail.sha").unwrap();
let mut buf = BufWriter::new(file);
buf.write_all(canonized.as_bytes()).unwrap();
let computed_sha = openssl::sha::sha256(canonized.as_ref());
}
We
- write the content of the canonized string into the
Mail.sha
file, and - compute the sha256 checksum with openssl crate.
However, when I compare computed_sha withsha256sum Mail.sha
they are not the same.
Can anyone help me to find the issue?
Reproduce the problem:
cargo add openssl
cargo add base64
use base64::engine::general_purpose::STANDARD;
fn relaxed_canonicalize_body(body: &str) -> String {
let lines: Vec<&str> = body.lines().collect();
let mut canonicalized_body = String::new();
for line in lines {
if line.as_bytes() == b"\r\n" || line.is_empty() {
continue;
}
let normalized_line = line.split_whitespace().collect::<Vec<&str>>().join(" ");
canonicalized_body.push_str(&format!("{}\r\n", normalized_line.trim_end()));
}
// Remove trailing CRLF for relaxed canonicalization
while canonicalized_body.ends_with("\r\n") {
canonicalized_body.pop();
canonicalized_body.pop();
}
canonicalized_body.push_str("\r\n");
dbg!(&canonicalized_body);
canonicalized_body
}
#[test]
fn custom_body_test() {
let body = include_str!("Mail.eml");
let canonized = relaxed_canonicalize_body(body);
dbg!(&canonized);
let computed_sha = openssl::sha::sha256(canonized.as_ref());
let file = std::fs::File::create("Mail.sha").unwrap();
let mut buf = BufWriter::new(file);
buf.write_all(canonized.as_bytes()).unwrap();
let computed_base64 = STANDARD.encode(computed_sha);
dbg!(computed_base64);
}
The content of Mail.emp:
b"Mail\r\nempty\r\n"