Crypto

Defines C API for calculating and checking hash.

Functions

Functions Documentation

function assert_sha256

void assert_sha256(
    const char * data,
    uint32_t length,
    const struct capi_checksum256 * hash
)

Tests if the sha256 hash generated from data matches the provided checksum.

Parameters:

  • data - Data you want to hash
  • length - Data length
  • hash - capi_checksum256* hash to compare to

Precondition:

assert256 hash of data equals provided hash parameter.

Post

Executes next statement. If was not true, hard return.

Note:

This method is optimized to a NO-OP when in fast evaluation mode.

Example:

checksum hash;
char data;
uint32_t length;
assert_sha256( data, length, hash )
//If the sha256 hash generated from data does not equal provided hash, anything below will never fire.
eosio::print("sha256 hash generated from data equals provided hash");

function assert_sha1

void assert_sha1(
    const char * data,
    uint32_t length,
    const struct capi_checksum160 * hash
)

Tests if the sha1 hash generated from data matches the provided checksum.

Note:

This method is optimized to a NO-OP when in fast evaluation mode.

Parameters:

  • data - Data you want to hash
  • length - Data length
  • hash - capi_checksum160* hash to compare to

Precondition:

sha1 hash of data equals provided hash parameter.

Post

Executes next statement. If was not true, hard return.

Example:

checksum hash;
char data;
uint32_t length;
assert_sha1( data, length, hash )
//If the sha1 hash generated from data does not equal provided hash, anything below will never fire.
eosio::print("sha1 hash generated from data equals provided hash");

function assert_sha512

void assert_sha512(
    const char * data,
    uint32_t length,
    const struct capi_checksum512 * hash
)

Tests if the sha512 hash generated from data matches the provided checksum.

Note:

This method is optimized to a NO-OP when in fast evaluation mode.

Parameters:

  • data - Data you want to hash
  • length - Data length
  • hash - capi_checksum512* hash to compare to

Precondition:

assert512 hash of data equals provided hash parameter.

Post

Executes next statement. If was not true, hard return.

Example:

checksum hash;
char data;
uint32_t length;
assert_sha512( data, length, hash )
//If the sha512 hash generated from data does not equal provided hash, anything below will never fire.
eosio::print("sha512 hash generated from data equals provided hash");

function assert_ripemd160

void assert_ripemd160(
    const char * data,
    uint32_t length,
    const struct capi_checksum160 * hash
)

Tests if the ripemod160 hash generated from data matches the provided checksum.

Parameters:

  • data - Data you want to hash
  • length - Data length
  • hash - capi_checksum160* hash to compare to

Precondition:

assert160 hash of data equals provided hash parameter.

Post

Executes next statement. If was not true, hard return.

Example:

checksum hash;
char data;
uint32_t length;
assert_ripemod160( data, length, hash )
//If the ripemod160 hash generated from data does not equal provided hash, anything below will never fire.
eosio::print("ripemod160 hash generated from data equals provided hash");

function sha256

void sha256(
    const char * data,
    uint32_t length,
    struct capi_checksum256 * hash
)

Hashes data using sha256 and stores result in memory pointed to by hash.

Parameters:

  • data - Data you want to hash
  • length - Data length
  • hash - Hash pointer

Example:

checksum calc_hash;
sha256( data, length, &calc_hash );
eos_assert( calc_hash == hash, "invalid hash" );

function sha1

void sha1(
    const char * data,
    uint32_t length,
    struct capi_checksum160 * hash
)

Hashes data using sha1 and stores result in memory pointed to by hash.

Parameters:

  • data - Data you want to hash
  • length - Data length
  • hash - Hash pointer

Example:

checksum calc_hash;
sha1( data, length, &calc_hash );
eos_assert( calc_hash == hash, "invalid hash" );

function sha512

void sha512(
    const char * data,
    uint32_t length,
    struct capi_checksum512 * hash
)

Hashes data using sha512 and stores result in memory pointed to by hash.

Parameters:

  • data - Data you want to hash
  • length - Data length
  • hash - Hash pointer

Example:

checksum calc_hash;
sha512( data, length, &calc_hash );
eos_assert( calc_hash == hash, "invalid hash" );

function ripemd160

void ripemd160(
    const char * data,
    uint32_t length,
    struct capi_checksum160 * hash
)

Hashes data using ripemod160 and stores result in memory pointed to by hash.

Parameters:

  • data - Data you want to hash
  • length - Data length
  • hash - Hash pointer

Example:

checksum calc_hash;
ripemod160( data, length, &calc_hash );
eos_assert( calc_hash == hash, "invalid hash" );

function recover_key

int recover_key(
    const struct capi_checksum256 * digest,
    const char * sig,
    size_t siglen,
    char * pub,
    size_t publen
)

Calculates the public key used for a given signature and hash used to create a message.

Parameters:

  • digest - Hash used to create a message
  • sig - Signature
  • siglen - Signature length
  • pub - Public key
  • publen - Public key length

Returns:

int - number of bytes written to pub

Example:

function assert_recover_key

void assert_recover_key(
    const struct capi_checksum256 * digest,
    const char * sig,
    size_t siglen,
    const char * pub,
    size_t publen
)

Tests a given public key with the generated key from digest and the signature.

Parameters:

  • digest - What the key will be generated from
  • sig - Signature
  • siglen - Signature length
  • pub - Public key
  • publen - Public key length

Precondition:

assert recovery key of pub equals the key generated from the digest parameter

Post

Executes next statement. If was not true, hard return.

Example:

checksum digest;
char sig;
size_t siglen;
char pub;
size_t publen;
assert_recover_key( digest, sig, siglen, pub, publen )
// If the given public key does not match with the generated key from digest and the signature, anything below will never fire.
eosio::print("pub key matches the pub key generated from digest");