diff options
author | Dimitri Staessens <[email protected]> | 2023-08-16 18:15:14 +0200 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2023-08-23 13:09:11 +0200 |
commit | ab8ee0b24335790e49127b1330f9d7bcca7f6bfa (patch) | |
tree | b58fa560f7bf52d951547522f9b6d87244af3e06 /include | |
parent | de9674f090c2ae71cacd5f8bd6dcf5e8657ad686 (diff) | |
download | ouroboros-ab8ee0b24335790e49127b1330f9d7bcca7f6bfa.tar.gz ouroboros-ab8ee0b24335790e49127b1330f9d7bcca7f6bfa.zip |
include: Revise printing hashes
The code was a bit convoluted to print hashes as hex strings. Renamed
to HASH_FMT32 and HASH_VAL32 to make clear we are printing the first
32 bits only, and added options to print 64 up to 512 bits as well.
This doesn't depend on endianness anymore. Adds a small test for the
hash (printing) functions.
Signed-off-by: Dimitri Staessens <[email protected]>
Signed-off-by: Sander Vrijders <[email protected]>
Diffstat (limited to 'include')
-rw-r--r-- | include/ouroboros/hash.h | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/include/ouroboros/hash.h b/include/ouroboros/hash.h index 70d2eba0..6e438f50 100644 --- a/include/ouroboros/hash.h +++ b/include/ouroboros/hash.h @@ -25,7 +25,6 @@ #include "config.h" -#include <ouroboros/endian.h> #include <ouroboros/ipcp.h> #include <stdint.h> @@ -41,12 +40,35 @@ enum hash_algo { HASH_MD5, }; -#define HASH_FMT "%02x%02x%02x%02x" -#define HASH_VAL(hash) \ - (betoh32(*(uint32_t *) hash) & 0xFF000000) >> 24, \ - (betoh32(*(uint32_t *) hash) & 0x00FF0000) >> 16, \ - (betoh32(*(uint32_t *) hash) & 0x0000FF00) >> 8, \ - (betoh32(*(uint32_t *) hash) & 0x000000FF) +#define HASH_FMT32 "%02x%02x%02x%02x" +#define HASH_VAL32(hash) \ + (hash)[0], (hash)[1], (hash)[2], (hash)[3] + +#define HASH_FMT64 HASH_FMT32 HASH_FMT32 +#define HASH_VAL64(hash64) \ + HASH_VAL32(hash64), HASH_VAL32(hash64 + 4) + +#define HASH_FMT128 HASH_FMT64 HASH_FMT64 +#define HASH_VAL128(hash128) \ + HASH_VAL64(hash128), HASH_VAL64(hash128 + 8) + +#define HASH_FMT224 HASH_FMT128 HASH_FMT64 HASH_FMT32 +#define HASH_VAL224(hash224) \ + HASH_VAL128(hash224), HASH_VAL64(hash224 + 16), \ + HASH_VAL32(hash224 + 24) + +#define HASH_FMT256 HASH_FMT128 HASH_FMT128 +#define HASH_VAL256(hash256) \ + HASH_VAL128(hash256), HASH_VAL128(hash256 + 16) + +#define HASH_FMT384 HASH_FMT256 HASH_FMT128 +#define HASH_VAL384(hash384) \ + HASH_VAL256(hash384), HASH_VAL128(hash384 + 32) + +#define HASH_FMT512 HASH_FMT256 HASH_FMT256 +#define HASH_VAL512(hash512) \ + HASH_VAL256(hash512), HASH_VAL256(hash512 + 32) + uint16_t hash_len(enum hash_algo algo); |