I am hashing a string.
let src = String::from("abcd12342020");
let h1 = sha2::Sha256::digest(&src.as_bytes());
let h2 = sha2::Sha256::digest(hex::decode(&src).expect("this is not hex"));
h1 and h2 are different. What is the underlying reason? What is the difference between a bytes representation and the hex decoded one?
(Tried to provide a playground but the hex create is not available there)
h1andh2are different becausesrc.as_bytes()andhex::decode(&src)are different. Wrap them with thedbg!macro to see the difference.They're different because
hex::decodeparses the text as hex, meaningabgets parsed into0xABetc. Butsrc.as_bytes()returns the text encoded as a UTF-8 byte sequence, meaningabis encoded in memory as[0x61, 0x62].