Nodejs vs Rust sha256 for images

Not strictly related to the IC but I compare sha256 computed locally in NodeJS and those I compute in my canister in Rust and I get different values for images and fonts.

I got same values for file like HTML, CSS and JS (raw or gzipped) but not images and fonts.

In NodeJS I do:

const computeSha256 = async (file: string): Promise<string> => {
  const buffer = await readFile(file);
  const key = buffer.toString('utf-8');
  return crypto.createHash('sha256').update(key).digest('base64');
};

In Rust:

fn from(content_chunks: &Vec<Vec<u8>>) -> Self {
        let mut total_length: u128 = 0;
        let mut hasher = Sha256::new();

        for chunk in content_chunks.iter() {
            total_length += u128::try_from(chunk.len()).unwrap();

            hasher.update(chunk);
        }

        let sha256 = hasher.finalize().into();

An idea?

3 Likes

Issue was between the keyboard and the screen :man_facepalming:.

Should not have converted the buffer to string in NodeJS.

const computeSha256 = async (file: string): Promise<string> => {
  const buffer = await readFile(file);
  return crypto.createHash('sha256').update(buffer).digest('base64');
};
3 Likes