picofuse

Macros | Typedefs | Enumerations

Incremental MD5 and SHA-256 hash generation, sometimes with hardware acceleration. More...

Collaboration diagram for Hashes:

Macros

#define SYS_HASH_SIZE   32
 Maximum digest buffer size in bytes.
 
#define SYS_HASH_CAPACITY   4
 Maximum number of concurrent hash contexts available.
 

Typedefs

typedef enum sys_hash_algorithm_t sys_hash_algorithm_t
 Hash algorithm identifiers.
 
typedef struct sys_hash_t sys_hash_t
 Hash context.
 

Enumerations

enum  sys_hash_algorithm_t { sys_hash_md5 = 1, sys_hash_sha256 }
 Hash algorithm identifiers. More...
 

Lifecycle

sys_hash_tsys_hash_init (sys_hash_algorithm_t algorithm)
 Initialize a new hash context. More...
 
void sys_hash_deinit (sys_hash_t *hash)
 Release a hash context. More...
 

Methods

size_t sys_hash_size (const sys_hash_t *hash)
 Return the digest size in bytes for a hash context. More...
 
bool sys_hash_update (sys_hash_t *hash, sys_iostream_t *stream, size_t size)
 Update a hash context by reading more data from a stream. More...
 
const uint8_t * sys_hash_finalize (sys_hash_t *hash)
 Finalize the hash computation. More...
 
uintptr_t sys_hash_djb2 (const char *str)
 Compute a djb2 hash for a NULL-terminated string. More...
 

Detailed Description

Incremental MD5 and SHA-256 hash generation, sometimes with hardware acceleration.

MD5 is not considered secure, but it is still useful for checksums and other non-security purposes. SHA-256 is recommended for security-sensitive uses and may have hardware acceleration on some platforms.

Enumeration Type Documentation

◆ sys_hash_algorithm_t

Hash algorithm identifiers.

Enumerator
sys_hash_md5 

MD5 digest.

sys_hash_sha256 

SHA-256 digest.

Definition at line 54 of file hash.h.

54  {
55  sys_hash_md5 = 1,
MD5 digest.
Definition: hash.h:55
sys_hash_algorithm_t
Hash algorithm identifiers.
Definition: hash.h:54
SHA-256 digest.
Definition: hash.h:56

Function Documentation

◆ sys_hash_deinit()

void sys_hash_deinit ( sys_hash_t hash)

Release a hash context.

Parameters
hashPointer to the hash context to release.

Releases all resources associated with the hash context and renders it unusable. After this call, the handle must not be used again.

◆ sys_hash_djb2()

uintptr_t sys_hash_djb2 ( const char *  str)

Compute a djb2 hash for a NULL-terminated string.

Parameters
strThe NULL-terminated string to hash.
Returns
The computed hash value.

◆ sys_hash_finalize()

const uint8_t* sys_hash_finalize ( sys_hash_t hash)

Finalize the hash computation.

Parameters
hashPointer to the hash context.
Returns
Pointer to the computed digest stored inside the hash context, or NULL on failure.

The returned pointer remains owned by the hash context and is valid until sys_hash_deinit() is called for that handle.

◆ sys_hash_init()

sys_hash_t* sys_hash_init ( sys_hash_algorithm_t  algorithm)

Initialize a new hash context.

Parameters
algorithmThe hash algorithm to use.
Returns
Initialized hash context, or NULL if the algorithm is unsupported or no implementation capacity remains.

Allocates and initializes a hash context from an implementation-defined static capacity. The returned handle is ready for use with sys_hash_update() and sys_hash_finalize(). Call sys_hash_deinit() to release the slot for reuse.

◆ sys_hash_size()

size_t sys_hash_size ( const sys_hash_t hash)

Return the digest size in bytes for a hash context.

Parameters
hashPointer to the hash context.
Returns
The digest size in bytes, or 0 if the context is invalid.

◆ sys_hash_update()

bool sys_hash_update ( sys_hash_t hash,
sys_iostream_t stream,
size_t  size 
)

Update a hash context by reading more data from a stream.

Parameters
hashPointer to the hash context.
streamThe stream to read from.
sizeNumber of bytes to read from stream and feed into the hash, or 0 to read until end of stream. Call this repeatedly (e.g. once per chunk as it becomes available, each with an explicit size) to hash a stream incrementally, or once with size 0 to hash all of its remaining bytes in one call.
Returns
true on success, false on error - including a short read (stream had fewer than size bytes left, when size is nonzero).