CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Digest Functions

Data Structures

struct  Digest
 

Macros

#define DIGEST_BLOCKSIZE   64
 Internal block size in bytes used by all supported digest algorithms.
 

Typedefs

typedef struct Digest Digest
 

Enumerations

enum  DigestType { DIGEST_MD5 , DIGEST_SHA1 , DIGEST_SHA256 , DIGEST_COUNT }
 Supported message digest algorithms. More...
 

Functions

void digestInit (Digest *digest, DigestType type)
 
void digestUpdate (Digest *digest, uint8 *data, uint32 size)
 
void digestFinish (Digest *digest, uint8 *out)
 

Variables

uint32 DigestSize [DIGEST_COUNT]
 

Detailed Description

Cryptographic hash functions including MD5, SHA-1, and SHA-256.

The digest API provides incremental hashing through a simple three-step process:

  1. Initialize a context with digestInit()
  2. Feed data in chunks with digestUpdate() (can be called multiple times)
  3. Finalize and retrieve the hash with digestFinish()

All supported algorithms use a 64-byte internal block size and can process data of any length. Contexts can be reused by calling digestInit() again.

Example:

Digest ctx;
digestUpdate(&ctx, (uint8*)"Hello ", 6);
digestUpdate(&ctx, (uint8*)"World", 5);
uint8 hash[32];
digestFinish(&ctx, hash);
// hash now contains the SHA-256 of "Hello World"
void digestFinish(Digest *digest, uint8 *out)
void digestInit(Digest *digest, DigestType type)
void digestUpdate(Digest *digest, uint8 *data, uint32 size)
@ DIGEST_SHA256
SHA-256 - 256-bit (32 bytes) - Secure and recommended.
Definition digest.h:40

Typedef Documentation

◆ Digest

typedef struct Digest Digest

Digest context structure.

Contains the state for computing a message digest incrementally. All fields are internal and should not be accessed directly.

Enumeration Type Documentation

◆ DigestType

enum DigestType

Supported message digest algorithms.

Enumerator
DIGEST_MD5 

MD5 - 128-bit (16 bytes) - Not cryptographically secure.

DIGEST_SHA1 

SHA-1 - 160-bit (20 bytes) - Deprecated for security.

DIGEST_SHA256 

SHA-256 - 256-bit (32 bytes) - Secure and recommended.

Definition at line 37 of file digest.h.

Function Documentation

◆ digestFinish()

void digestFinish ( Digest digest,
uint8 *  out 
)

Finalizes the digest computation and outputs the hash.

Completes the digest computation by adding padding and length as required by the algorithm specification, then outputs the final hash value. After calling this function, digestInit() must be called again to reuse the context.

Parameters
digestDigest context that has been updated with data
outBuffer to receive the hash output, must be at least DigestSize[type] bytes

Example:

uint8 hash[32]; // SHA-256 produces 32 bytes
digestFinish(&ctx, hash);

◆ digestInit()

void digestInit ( Digest digest,
DigestType  type 
)

Initializes a digest context for computing a message digest.

Must be called before using digestUpdate() or digestFinish(). The context can be reused after digestFinish() by calling digestInit() again.

Parameters
digestDigest context to initialize
typeAlgorithm to use (DIGEST_MD5, DIGEST_SHA1, or DIGEST_SHA256)

Example:

◆ digestUpdate()

void digestUpdate ( Digest digest,
uint8 *  data,
uint32  size 
)

Incrementally processes data for the message digest.

Can be called multiple times to hash data in chunks. All data passed to digestUpdate() is accumulated until digestFinish() is called to produce the final hash.

Parameters
digestInitialized digest context
dataData to process
sizeNumber of bytes to process

Example:

digestUpdate(&ctx, (uint8*)"Hello ", 6);
digestUpdate(&ctx, (uint8*)"World", 5);

Variable Documentation

◆ DigestSize

uint32 DigestSize[DIGEST_COUNT]
extern

Size in bytes of the resulting digest for each algorithm. Index with DigestType: DigestSize[DIGEST_MD5] = 16, DigestSize[DIGEST_SHA1] = 20, DigestSize[DIGEST_SHA256] = 32