eos
eos copied to clipboard
How to use C API for crypto in smart contract?
I have problem when i use sha256 API, here is my code:
#include <eosiolib/eosio.hpp>
#include <eosiolib/crypto.h>
#include
[[eosio::action]]
extern "C" {
char data=28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905;
void sha256( char* data, uint32_t length, checksum256* hash ){
checksum calc_hash;
sha256( data, length, &calc_hash );
eos_assert( calc_hash == hash, "invalid hash" );
} } };
~/eosio.cdt/demo1$ eosio-cpp init.cpp -o init.wasm --abigen init.cpp:12:14: error: expected member name or ';' after declaration specifiers extern "C" { ~~~~~~ ^ 1 error generated.
This is an example for your data:
#include <eosiolib/crypto.h> // sha256 api
...
...
/// @abi action
void shatest() {
unsigned char raw[] = {0x28, 0x34, 0x9b, 0x1d, 0x4b, 0xcd, 0xc9, 0x90, 0x5e, 0x4e, 0xf9, 0x71, 0x90, 0x19, 0xe5, 0x57, 0x43, 0xc8, 0x4e, 0xfa, 0x0c, 0x5e, 0x9a, 0x0b, 0x07, 0x7f, 0x0b, 0x54, 0xfc, 0xd8, 0x49, 0x05};
checksum256 hash;
sha256((const char *)raw, sizeof(raw), &hash);
printhex((const char *)&hash, sizeof(hash));
}
And the corresponding result:
executed transaction: d1ea54fd544093f9c91bb72a0dff6fd253438e416c96198b0b94c14eec09b34d 96 bytes 376 us
# a123 <= a123::shatest ""
>> d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883
warning: transaction executed locally, but may not be confirmed by the network yet ]
You can double check your hash value from http://extranet.cryptomathic.com/hashcalc/index
@taokayan OK,Thanks