Hello list.<div><br></div><div>Came across some weird behaviour when attempting a simple openssl ffi which I cannot reproduce in C alone.</div><div><br></div><div>Attached is the scheme code and the (more or less) equivalent C code.</div>
<div><br></div><div>The output of the scheme code is as follows:</div><div><div>81b637d8fcd2c6da6359e6963113a1170de795e4b725b84d1e0b4cfd9ec58ce9</div><div>81b637d8fcd2c6da6359e6963113a1170de795e4b725b84d1e0b4cfd9ec58ce9</div>
<div>8131333133333331333333333333333133333333333333333333333333333331</div><div>8131333133333331333333333333333133333333333333333333333333333331</div></div><div><br></div><div>The first line is the correct sha256 hash of the string "bob" from the sha256-final form.</div>
<div>The second line is what should be (and is) the correct hash based on the byte array returned from sha256-final.</div><div>The third line is the erroneous hash as a result of storing the hash string using sprintf using the same byte array as before.</div>
<div>The final line is printing the resulting hash string from within scheme which should be the same as the third line (which it is and so is 'correctly' erroneous :).</div><div><br></div><div>The first problem/question is: why is sprintf screwing up?  And why does it only screw up in bytes->string and not in sha256_final?</div>
<div><br></div><div>The second problem occurs when you swap the order of the two loops in bytes->string after which the output is:</div><div><div>81b637d8fcd2c6da6359e6963113a1170de795e4b725b84d1e0b4cfd9ec58ce9</div><div>
3831333133333331333333333333333133333333333333333333333333333331</div><div>8131333133333331333333333333333133333333333333333333333333333331</div><div>8131333133333331333333333333333133333333333333333333333333333331</div></div>
<div><br></div><div>Why the hell is the second line now completely screwed up?</div><div><br></div><div>Interesting to note that the first hex value (81) is correct in the erroneous (sprintf) strings, yet after swapping the two loops around the first hex value (now 38) is different even though the rest of the erroneous hash string is the same.</div>
<div><br></div><div>Please release me from this madness.</div><div><br></div><div>PS: remember to pass "-lcrypto" when compiling.</div>