Openssl - Sha checksum of file is not the same with sha256sum command

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

  1. write the content of the canonized string into the Mail.sha file, and
  2. compute the sha256 checksum with openssl crate.
    However, when I compare computed_sha with sha256sum 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"


image

Those are the same, but one is displayed in Base64 and the other is displayed in Hex. Try the below or so.

computed_sha.iter().for_each(|byte| print!("{byte:02x}"));
println!();

Thanks for your answer!
even if you print dbg!(computed_sha);, it is still not the same when you do the process with sha256sum | base64.
There is also another issue, I ask this community for help.

That does not show the base64 representation of the 32 bytes of the computed sum.

That shows base64 representation of the ASCII output of the sha25sum command, including the spaces, filename, and newline (67 + filename.len() bytes).

1 Like