Bitcoin Forum

Other => Off-topic => Topic started by: curiosity81 on December 21, 2017, 11:44:55 AM



Title: Why is the result of sha256 the same for inputs 0 to 15?
Post by: curiosity81 on December 21, 2017, 11:44:55 AM
Just for curiosity:

Why does

Code:
echo "obase=16;ibase=10;$var" | bc | xxd -p -r | sha256sum

result in

Code:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

where $var is in {"0", ..., "15"}?

Thx.


Title: Re: Why is the result of sha256 the same for inputs 0 to 15?
Post by: Foxpup on December 21, 2017, 12:20:11 PM
Because converting hex digits into bytes requires an even number of the former, since each byte requires two hex digits. In particular, xxd drops the last hex digit if there are an odd number of them, and if this results in a zero-length input, it will produce a zero-length output (compare your hash to the output of "echo -n | sha256sum").


Title: Re: Why is the result of sha256 the same for inputs 0 to 15?
Post by: curiosity81 on December 21, 2017, 12:22:07 PM
Because converting hex digits into bytes requires an even number of the former, since each byte requires two hex digits. In particular, xxd drops the last hex digit if there are an odd number of them, and if this results in a zero-length input, it will produce a zero-length output (compare your hash to the output of "echo -n | sha256sum").

I see. This makes sense! :D

Thank you very much.


Title: Re: Why is the result of sha256 the same for inputs 0 to 15?
Post by: curiosity81 on December 21, 2017, 12:27:24 PM
So, to do it correctly, if bc returns an odd number of digits, would it be correct to add a 0 at the beginning of the hex-string?


Title: Re: Why is the result of sha256 the same for inputs 0 to 15?
Post by: Foxpup on December 21, 2017, 12:34:26 PM
Yes.


Title: Re: Why is the result of sha256 the same for inputs 0 to 15?
Post by: curiosity81 on December 21, 2017, 12:40:39 PM
Thx again.