Methods for hash generation, sometimes with hardware acceleration.
More...
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.
◆ 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
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.
◆ 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.
sys_hash_algorithm_t
Hash algorithm identifiers.
MD5 hash algorithm (128-bit output, not cryptographically secure)
SHA-256 hash algorithm (256-bit output, cryptographically secure)
◆ sys_hash_djb2()
uintptr_t sys_hash_djb2 |
( |
const char * |
str | ) |
|
djb2 hash function for strings
- Parameters
-
str | The 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
-
hash | The 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()
Initializes a new hash context for the specified algorithm.
- Parameters
-
algorithm | The 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()
Returns the size of the hash in bytes.
- Parameters
-
hash | The 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
-
hash | The hash context to update. |
data | The data to add to the hash. |
size | The size of the data in bytes. |
- Returns
- true on success, false on failure.