objc

Data Structures | Macros | Enumerations | Functions

Methods for hash generation, sometimes with hardware acceleration. More...

Collaboration diagram for Hashes:

Data Structures

struct  sys_hash_t
 Hash context structure. More...
 

Macros

#define SYS_HASH_SIZE   32
 Size of the hash. More...
 
#define SYS_HASH_CTX_SIZE   128
 Size of the hash context buffer. More...
 

Enumerations

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

Functions

sys_hash_t sys_hash_init (sys_hash_algorithm_t algorithm)
 Initializes a new hash context for the specified algorithm. More...
 
size_t sys_hash_size (sys_hash_t *hash)
 Returns the size of the hash in bytes. More...
 
bool sys_hash_update (sys_hash_t *hash, const void *data, size_t size)
 Updates the hash context with new data. More...
 
const uint8_t * sys_hash_finalize (sys_hash_t *hash)
 Finalizes the hash computation and returns the hash value. More...
 
uintptr_t sys_hash_djb2 (const char *str)
 djb2 hash function for strings More...
 

Detailed Description

Methods for hash generation, sometimes with hardware acceleration.

This file declares types and functions for generating cryptographic hashes from data. It supports MD5 and SHA-256 hashing algorithms, the former of which (I have been told to tell you) is not secure, but is still widely used for checksums and non-security purposes. SHA-256 is a more secure algorithm and is recommended for security-sensitive applications, and often has hardware acceleration on modern platforms.

In order to hash an arbitrary amount of data, you must first initialize a sys_hash_t context using sys_hash_init(), then you can call sys_hash_update() multiple times with chunks of data, and finally call sys_hash_finalize() to get the final hash value. The sys_hash_t context can be reused for multiple hashes, but you must call sys_hash_finalize() to clean it up after each use.

Macro Definition Documentation

◆ SYS_HASH_CTX_SIZE

#define SYS_HASH_CTX_SIZE   128

Size of the hash context buffer.

This defines the size of the context buffer used for hash operations. It should be large enough to hold any hash algorithm's context.

Definition at line 48 of file hash.h.

◆ SYS_HASH_SIZE

#define SYS_HASH_SIZE   32

Size of the hash.

This defines the size of the hash value produced by the hash operations. It should be large enough to hold any hash algorithm's output.

Definition at line 39 of file hash.h.

Enumeration Type Documentation

◆ sys_hash_algorithm_t

Hash algorithm identifiers.

Enumeration of supported cryptographic hash algorithms.

Enumerator
sys_hash_md5 

MD5 hash algorithm (128-bit output, not cryptographically secure)

sys_hash_sha256 

SHA-256 hash algorithm (256-bit output, cryptographically secure)

Definition at line 56 of file hash.h.

56  {
57  sys_hash_md5 =
58  1,
sys_hash_algorithm_t
Hash algorithm identifiers.
Definition: hash.h:56
MD5 hash algorithm (128-bit output, not cryptographically secure)
Definition: hash.h:57
SHA-256 hash algorithm (256-bit output, cryptographically secure)
Definition: hash.h:59

Function Documentation

◆ sys_hash_djb2()

uintptr_t sys_hash_djb2 ( const char *  str)

djb2 hash function for strings

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

◆ sys_hash_finalize()

const uint8_t* sys_hash_finalize ( sys_hash_t hash)

Finalizes the hash computation and returns the hash value.

Parameters
hashThe hash context to finalize.
Returns
A pointer to the computed hash value, or NULL on failure.

This function finalizes the hash computation and returns a pointer to the computed hash value stored in the sys_hash_t instance. The context is automatically cleaned up after finalization.

◆ sys_hash_init()

sys_hash_t sys_hash_init ( sys_hash_algorithm_t  algorithm)

Initializes a new hash context for the specified algorithm.

Parameters
algorithmThe hash algorithm to use.
Returns
A new sys_hash_t instance initialized for the specified algorithm.

This function returns a sys_hash_t instance for the specified hash algorithm, ready for use in hashing operations. You must call sys_hash_final() to finalize the hash computation, even on failure of sys_hash_update().

Note
If a specific algorithm is not supported, the context will be initialized with size 0. You should check sys_hash_size() to verify if the context is usable.

◆ sys_hash_size()

size_t sys_hash_size ( sys_hash_t hash)

Returns the size of the hash in bytes.

Parameters
hashThe hash context to query.
Returns
The size of the hash in bytes.

This function returns the size of the hash value stored in the sys_hash_t instance, which is determined by the algorithm used. If the hashing failed, the size will be 0.

◆ sys_hash_update()

bool sys_hash_update ( sys_hash_t hash,
const void *  data,
size_t  size 
)

Updates the hash context with new data.

Parameters
hashThe hash context to update.
dataThe data to add to the hash.
sizeThe size of the data in bytes.
Returns
true on success, false on failure.