From ab8ee0b24335790e49127b1330f9d7bcca7f6bfa Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Wed, 16 Aug 2023 18:15:14 +0200 Subject: 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 Signed-off-by: Sander Vrijders --- include/ouroboros/hash.h | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'include/ouroboros') 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 #include #include @@ -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); -- cgit v1.2.3