0

I have a problem, when I was in the 1.3 version of eosio.CDT I successfully reproduce the same SHA256 in my contract and with multiple external method.

It work like the dice contract. You generate in private a random key and his hash. You send the hash first and you validate it with the key send later.

In the table, I save the hash like this

capi_checksum256 deliveryverification;

With the update I have to do like this

checksum256 deliveryverification;

And when I'm making the verification I did like this

void rideos::orderdelive(const uint64_t orderKey, const capi_checksum256 &source)
{
auto iteratorOrder = _orders.find(orderKey);
eosio_assert(iteratorOrder != _orders.end(), "Address for order not found");

require_auth(iteratorOrder->deliver);

assert_sha256((char *)&source, sizeof(source), (const capi_checksum256 *)&iteratorOrder->deliveryverification);
}

It worked perfectly and I had the same result of the generated sha256 with bash

keyTester=$(openssl rand -hex 32)
hashTester=$(echo -n $keyTester | xxd -r -p | sha256sum -b | awk '{print $1}')

or with ecc-js

hash: ecc.sha256(new Buffer(this.state.key, 'hex'))

Now, my contract look like this

void rideos::orderdelive(const uint64_t orderKey, const string source)
{
auto iteratorOrder = _orders.find(orderKey);
check(iteratorOrder != _orders.end(), "Address for order not found");

require_auth(iteratorOrder->deliver);

eosio::print("============================\n");
eosio::print(source);
eosio::print("\n");
eosio::print(sha256((char *)source.c_str(), source.size()));
eosio::print("\n");
eosio::print(sha256(const_cast<char *>(source.c_str()), source.size()));
eosio::print("\n");
eosio::print(iteratorOrder->deliveryverification);

assert_sha256((char *)&source, sizeof(source), iteratorOrder->deliveryverification);
}

The output is : Error Details: hash mismatch pending console output: ============================ 9987ed96ebf6f52cab4fcd6f083d45793302b94c82bc359eb060e2a72d71cf61 fa41a6db58731d0d4374becc77a4e5697380027810e4a0479b40f15756d979a3 fa41a6db58731d0d4374becc77a4e5697380027810e4a0479b40f15756d979a3 a2ab06adb6e51204f60b6524b1305d62ad727d0d712b935963eba15e36f058dd

My key send is the first output and the generated sha256 in line 2-3 who should be the same than the hash line 4. In bash and with ecc I generate the same line 4 so what I missed in my contract ?

Thanks !

0

Originally, capi_checksum256 accepted hex string and converted it to binary. However, in your contract, you have changed capi_checksum256 to std::string, so the passed source argument will have ascii code of string instead of the binary that its hex string represents.

Your updated code needs to be like this:

void rideos::orderdelive(const uint64_t orderKey, const checksum256 &source) {
   auto iteratorOrder = _orders.find(orderKey);
   check(iteratorOrder != _orders.end(), "Address for order not found");

   require_auth(iteratorOrder->deliver);

   auto _source = source.extract_as_byte_array();

   assert_sha256((const char *)_source.data(), _source.size(), iteratorOrder->deliveryverification);
}
-1

The result with the new implementation of sha256 is just simple than before. in ecc-js simply use :

ecc.sha256(this.state.key)

and in shell, just remove the part with xxd

keyTester=$(openssl rand -hex 32)
hashTester=$(echo -n $keyTester | sha256sum -b | awk '{print $1}')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.