Skip to content

sha.h

Functions

Name
int wc_InitSha(wc_Sha * sha)
This function initializes SHA. This is automatically called by wc_ShaHash.
int wc_ShaUpdate(wc_Sha * sha, const byte * data, word32 len)
Can be called to continually hash the provided byte array of length len.
int wc_ShaFinal(wc_Sha * sha, byte * hash)
Finalizes hashing of data. Result is placed into hash. Resets state of sha struct.
void wc_ShaFree(wc_Sha * sha)
Used to clean up memory used by an initialized Sha struct.
int wc_ShaGetHash(wc_Sha * sha, byte * hash)
Gets hash data. Result is placed into hash. Does not reset state of sha struct.
int wc_InitSha_ex(wc_Sha * sha, void * heap, int devId)
Initializes SHA with heap and device ID.
int wc_ShaFinalRaw(wc_Sha * sha, byte * hash)
Gets raw hash without finalizing.
int wc_ShaCopy(wc_Sha * src, wc_Sha * dst)
Copies SHA context.
int wc_ShaTransform(wc_Sha * sha, const unsigned char * data)
Transforms SHA block.
void wc_ShaSizeSet(wc_Sha * sha, word32 len)
Sets SHA size.
int wc_ShaSetFlags(wc_Sha * sha, word32 flags)
Sets SHA flags.

Functions Documentation

function wc_InitSha

int wc_InitSha(
    wc_Sha * sha
)

This function initializes SHA. This is automatically called by wc_ShaHash.

Parameters:

  • sha pointer to the sha structure to use for encryption

See:

Return: 0 Returned upon successfully initializing

Example

Sha sha[1];
if ((ret = wc_InitSha(sha)) != 0) {
   WOLFSSL_MSG("wc_InitSha failed");
}
else {
   wc_ShaUpdate(sha, data, len);
   wc_ShaFinal(sha, hash);
}

function wc_ShaUpdate

int wc_ShaUpdate(
    wc_Sha * sha,
    const byte * data,
    word32 len
)

Can be called to continually hash the provided byte array of length len.

Parameters:

  • sha pointer to the sha structure to use for encryption
  • data the data to be hashed
  • len length of data to be hashed

See:

Return: 0 Returned upon successfully adding the data to the digest.

Example

Sha sha[1];
byte data[] = { // Data to be hashed };
word32 len = sizeof(data);

if ((ret = wc_InitSha(sha)) != 0) {
   WOLFSSL_MSG("wc_InitSha failed");
}
else {
   wc_ShaUpdate(sha, data, len);
   wc_ShaFinal(sha, hash);
}

function wc_ShaFinal

int wc_ShaFinal(
    wc_Sha * sha,
    byte * hash
)

Finalizes hashing of data. Result is placed into hash. Resets state of sha struct.

Parameters:

  • sha pointer to the sha structure to use for encryption
  • hash Byte array to hold hash value.

See:

Return: 0 Returned upon successfully finalizing.

Example

Sha sha[1];
byte data[] = { Data to be hashed };
word32 len = sizeof(data);

if ((ret = wc_InitSha(sha)) != 0) {
   WOLFSSL_MSG("wc_InitSha failed");
}
else {
   wc_ShaUpdate(sha, data, len);
   wc_ShaFinal(sha, hash);
}

function wc_ShaFree

void wc_ShaFree(
    wc_Sha * sha
)

Used to clean up memory used by an initialized Sha struct.

Parameters:

  • sha Pointer to the Sha struct to free.

See:

Return: No returns.

Example

Sha sha;
wc_InitSha(&sha);
// Use sha
wc_ShaFree(&sha);

function wc_ShaGetHash

int wc_ShaGetHash(
    wc_Sha * sha,
    byte * hash
)

Gets hash data. Result is placed into hash. Does not reset state of sha struct.

Parameters:

  • sha pointer to the sha structure to use for encryption
  • hash Byte array to hold hash value.

See:

Return: 0 Returned upon successfully finalizing.

Example

Sha sha[1];
if ((ret = wc_InitSha(sha)) != 0) {
WOLFSSL_MSG("wc_InitSha failed");
}
else {
    wc_ShaUpdate(sha, data, len);
    wc_ShaGetHash(sha, hash);
}

function wc_InitSha_ex

int wc_InitSha_ex(
    wc_Sha * sha,
    void * heap,
    int devId
)

Initializes SHA with heap and device ID.

Parameters:

  • sha SHA structure
  • heap Heap hint
  • devId Device ID

See: wc_InitSha

Return:

  • 0 on success
  • negative on error

Example

wc_Sha sha;
int ret = wc_InitSha_ex(&sha, NULL, INVALID_DEVID);

function wc_ShaFinalRaw

int wc_ShaFinalRaw(
    wc_Sha * sha,
    byte * hash
)

Gets raw hash without finalizing.

Parameters:

  • sha SHA structure
  • hash Output hash buffer

See: wc_ShaFinal

Return:

  • 0 on success
  • negative on error

Example

wc_Sha sha;
byte hash[WC_SHA_DIGEST_SIZE];
int ret = wc_ShaFinalRaw(&sha, hash);

function wc_ShaCopy

int wc_ShaCopy(
    wc_Sha * src,
    wc_Sha * dst
)

Copies SHA context.

Parameters:

  • src Source SHA structure
  • dst Destination SHA structure

See: wc_InitSha

Return:

  • 0 on success
  • negative on error

Example

wc_Sha src, dst;
int ret = wc_ShaCopy(&src, &dst);

function wc_ShaTransform

int wc_ShaTransform(
    wc_Sha * sha,
    const unsigned char * data
)

Transforms SHA block.

Parameters:

  • sha SHA structure
  • data Block data

See: wc_ShaUpdate

Return:

  • 0 on success
  • negative on error

Example

wc_Sha sha;
unsigned char block[WC_SHA_BLOCK_SIZE];
int ret = wc_ShaTransform(&sha, block);

function wc_ShaSizeSet

void wc_ShaSizeSet(
    wc_Sha * sha,
    word32 len
)

Sets SHA size.

Parameters:

  • sha SHA structure
  • len Size to set

See: wc_ShaUpdate

Return: none No returns

Example

wc_Sha sha;
wc_ShaSizeSet(&sha, 1000);

function wc_ShaSetFlags

int wc_ShaSetFlags(
    wc_Sha * sha,
    word32 flags
)

Sets SHA flags.

Parameters:

  • sha SHA structure
  • flags Flags to set

See: wc_InitSha

Return:

  • 0 on success
  • negative on error

Example

wc_Sha sha;
int ret = wc_ShaSetFlags(&sha, WC_HASH_FLAG_WILLCOPY);

Source code


int wc_InitSha(wc_Sha* sha);

int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len);

int wc_ShaFinal(wc_Sha* sha, byte* hash);

void wc_ShaFree(wc_Sha* sha);

int wc_ShaGetHash(wc_Sha* sha, byte* hash);
int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId);

int wc_ShaFinalRaw(wc_Sha* sha, byte* hash);

int wc_ShaCopy(wc_Sha* src, wc_Sha* dst);

int wc_ShaTransform(wc_Sha* sha, const unsigned char* data);

void wc_ShaSizeSet(wc_Sha* sha, word32 len);

int wc_ShaSetFlags(wc_Sha* sha, word32 flags);

Updated on 2025-12-31 at 01:16:04 +0000