2

I have following output in sha256 online:

Value as hex

But in my rust when i do

 let mut hasher = Sha256::default();

 hasher.update(input_data.secret);

 let input_secret_hash: String = format!("{:x}", hasher.finalize());

where input_data.secret == 75ca0e2daaf49840ddce9456378efbc95e97ed2566226edca2d73ca1c50450fc

i receive 46fe3a4fc1480dfdc1b23906c10cb49eee043733c6c410081aa245234c05e4cb

which is the value as text:

[Value as text2

How i'm able to receive value as the hex screenshot example

Andon Mitev
  • 1,354
  • 1
  • 11
  • 30
  • You need to [convert you hex string to a binary slice](https://stackoverflow.com/questions/52987181/how-can-i-convert-a-hex-string-to-a-u8-slice/52992629#52992629) before hashing it. – Sven Marnach Jun 14 '21 at 08:35

1 Answers1

2

Just decode your hex into the raw bytes:

use hex;
...
let message: Vec<u8> = hex::decode("75ca0e2daaf49840ddce9456378efbc95e97ed2566226edca2d73ca1c50450fc")
    .expect("Invalid Hex String");
...
hasher.update(&message);
...
Netwave
  • 40,134
  • 6
  • 50
  • 93