'Incorrect result of sha256 of a string in a Everscale Solidity smart contract

I would like to compute the sha256 of a string in a Free TON-Solidity contract, I do it by storing the string in a TvmBuilder and then passing it as a TvmSlice to sha256(), but the result is not correct (it doesn't match the one computed by sha256sum in my shell). Any idea why ?

Is the TvmBuilder adding some bits that are passed in the slice ?



Solution 1:[1]

Yes, tvm builder is kind of TL-B scheme serializer as far as I understand

sha256() function in Free TON Solidity API only takes a TvmBuilder as input, You can compute the hash for a raw string.

Hashing arbitrary string is hashing its BOC , because BOC is the only structure tvm can understand

i think you may want to build BOC out of this string. builder builds cells and cell layout is made of slices + refs. it results in a tree structure of slices mixed with refs, which resolve in blockchain state.

your approach should work for small strings as well as it works with the whole blockchain state. that’s the only way tvm understands data

so hash of string is hash of a cell which has proofs for underlying cells

that’s the way i now understand it, hope that helps.

and if you have string less than 127 bytes, you can pass bytes instead and hash bytes packed in a single cell

tg @freeton_smartcontracts here where clever SmC guys can clarify , because i’m self learner, not really hands on SmC pro

https://github.com/move-ton/ton-client-py/blob/b06b333e6f5582aa1888121cca80424b614e092c/tonclient/abi.py#L49

maybe this or rust core sdk help you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Konstantin