Search code examples
bashencryptioncryptographysha

Generated SHA256 value is different from the expected SHA256 value


The following steps are to be performed:

  1. Do base64 decode on ASCII value.
  2. To 'chirp' append the decoded value.
  3. Generate sha256 on the "chirp<decoded_value>"
    #!/bin/sh
    a=$(echo MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADAoR0WUZBTAkZv0Syvt+g5wGpb/HYHh22zAxCNP+ryTQ=|base64 -d)
    b="chirp$a"
    echo $b
    echo -n $b | sha256sum

I am getting a value of: f62e19108cfb5a91434f1bba9f5384f9039857743aa2c0707efaa092791e4420

But the expected value is: 6a29cb4....

Am I missing anything?


Solution

  • For binary data, as the base64-decoded data that your dealing with, I would not rely too much on echo, but just pipe the stuff, like this:

    <<<'MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADAoR0WUZBTAkZv0Syvt+g5wGpb/HYHh22zAxCNP+ryTQ=' base64 -d | cat <(echo -n chirp) - | sha256sum
    

    That gives me the result that you expect, 6a29cb438954e8c78241d786af874b1c7218490d3024345f6e11932377a932b6 .
    Here, cat gets two file descriptors as arguments, the first one streaming the word "chirp" and the second one forwarding the stdout of the previous command (base64 -d)