2

I actually use an implementation like the dice contract : https://github.com/EOSIO/eos/tree/v1.2.0/contracts/dice

You create a key and his hash with sha256, you first send the hash and then the key to the smart-contract. The contract use the function assert_sha256. I have difficulty for creating the hash with sha256 in Javascript. The encryption has to be the same than the EOS contract else assert_sha256 fail.

I try with the function sha256 of eosjs-ecc and other library without success.

In the dice contract example, they do it in shell like this

echo -n 'abc' | xxd -r -p | sha256sum -b | awk '{print $1}'

I don't understand all the parameters particularly xxd. It translates in binary ?

Thanks

EDIT :

Thanks Don, I spend a lot of time to understand how pass hex to binary, how xxd work, etc... but the answer was just in front of my eyes.

In the documentation of ecc : https://github.com/EOSIO/eosjs-ecc You just have to pass your hex key inside of this code

ecc.sha256(Buffer.from('02208b', 'hex'))

The first time, I probably didn't pass hex data so it's failed.

1
  • /akexabdre ah, gotcha. glad you figured it out.
    – Don
    Jan 9, 2019 at 3:19

1 Answer 1

2

xxd make a hexdump or do the reverse

-r revert, convert hexdump into binary

-p output 'plain hexdump style'

sha256sum compute and check SHA256 message digest

-b read in binary mode

Here the combination of -r -p options for the xxd command will read plain hexadecimal dumps without line number information and without a particular column layout allowing whitespace and linebreaks.

from the eosjs-ecc documentation:

sha256

Parameters

  • data (string | Buffer) always binary, you may need Buffer.from(data, 'hex')

  • resultEncoding (optional, default 'hex')

  • encoding string result encoding 'hex', 'binary' or 'base64' (optional, default 'hex')

Examples

ecc.sha256('hashme') === '02208b..',
ecc.sha256(Buffer.from('02208b', 'hex')) === '29a23..'

Returns (string | Buffer) Buffer when encoding is null, or string

There are also scripts & packages available for implementing sha256 algo. npm package

Depending on where/when you are trying to implement using javascript, there are some issues/concerns to consider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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