From 1b9e2168d8bff80202dee08a5e0aae878f94814c Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sun, 31 Jul 2016 19:31:19 +0100 Subject: [PATCH] create one info table from separate ones fix mh_hash_default_length --- src/hashes.c | 73 ++++++++++++++---------------------- tests/c/test_hashes_length.c | 2 +- 2 files changed, 30 insertions(+), 45 deletions(-) diff --git a/src/hashes.c b/src/hashes.c index a0caf80..c931a67 100644 --- a/src/hashes.c +++ b/src/hashes.c @@ -1,65 +1,50 @@ #include + #include "mh/hashes.h" #include "mh/errors.h" #include "mh/assert.h" -static const struct hash_name { +static const struct hash_info { int hash; const char *name; -} hash_names[] = { - { MH_H_SHA1, "sha1" }, - { MH_H_SHA2_256, "sha2-256" }, - { MH_H_SHA2_512, "sha2-512" }, - { MH_H_SHA3_512, "sha3-512" }, - { MH_H_SHA3_384, "sha3-384" }, - { MH_H_SHA3_256, "sha3-256" }, - { MH_H_SHA3_224, "sha3-224" }, - { MH_H_SHAKE_128, "shake-128" }, - { MH_H_SHAKE_256, "shake-256" }, - { MH_H_BLAKE2B, "blake2b" }, - { MH_H_BLAKE2S, "blake2s" } + int length; +} hash_infos[] = { + { MH_H_SHA1, "sha1", 20}, + { MH_H_SHA2_256, "sha2-256", 32 }, + { MH_H_SHA2_512, "sha2-512", 64 }, + { MH_H_SHA3_512, "sha3-512", 64 }, + { MH_H_SHA3_384, "sha3-384", 45 }, + { MH_H_SHA3_256, "sha3-256", 32 }, + { MH_H_SHA3_224, "sha3-224", 28 }, + { MH_H_SHAKE_128, "shake-128", 16 }, + { MH_H_SHAKE_256, "shake-256", 32 }, + { MH_H_BLAKE2B, "blake2b", 64 }, + { MH_H_BLAKE2S, "blake2s", 32 } }; -mh_assert_static(sizeof(hash_names) / sizeof(hash_names[0]) == MH_H_COUNT); +mh_assert_static(sizeof(hash_infos) / sizeof(hash_infos[0]) == MH_H_COUNT); -const char *mh_hash_name(int hash) { +// Searches for given hash in hash info table +static const struct hash_info *find_hash(int hash) { + // naive search, could be replaced with binary unsigned int i = 0; - for (; i < sizeof(mh_all_hashes) / sizeof(mh_all_hashes[0]); i++) { - if (hash_names[i].hash == hash) - return hash_names[i].name; - + for (; i < MH_H_COUNT; i++) { + if (hash_infos[i].hash == hash) + return &hash_infos[i]; } return NULL; } - -static const int hash_lengths[] = { - 20, // sha1 - 32, // sha2-256 - 64, // sha2-512 - 64, // sha3-512 - 45, // sha3-364 - 32, // sha3-256 - 28, // sha3-224 - 16, // shake-128 - 32, // shake-256 - 64, // blake2b - 32, // blake2s -}; - -mh_assert_static(sizeof(hash_lengths)/sizeof(hash_lengths[0]) == MH_H_COUNT); - -int mh_hash_default_length(int hash) { - if (hash < 0 || hash >= MH_H_COUNT) - return MH_E_UNKNOWN_CODE; - - return hash_lengths[hash]; +const char *mh_hash_name(int hash) { + const struct hash_info *info = find_hash(hash); + return info ? info->name : NULL; } -int mh_hash_length(const unsigned char multihash[], int len, int *hash_length); - -int mh_hash_function(const unsigned char multihash[], int len, int *hash); +int mh_hash_default_length(int hash) { + const struct hash_info *info = find_hash(hash); + return info ? info->length : MH_E_UNKNOWN_CODE; +} diff --git a/tests/c/test_hashes_length.c b/tests/c/test_hashes_length.c index 7dd8afb..dd02600 100644 --- a/tests/c/test_hashes_length.c +++ b/tests/c/test_hashes_length.c @@ -10,7 +10,7 @@ static char *test_all_hashes_have_lengths(void) { int i = 0; int length = 0; for (; i < MH_H_COUNT; i++) { - length = mh_hash_default_length(i); + length = mh_hash_default_length(mh_all_hashes[i]); sprintf(error_buf, "mh_hash_default_length: hash %d" " returned invalid (%d) default length", i, length);