2016-07-31 14:42:29 +00:00
|
|
|
#include <stdlib.h>
|
2016-07-31 18:31:19 +00:00
|
|
|
|
2016-07-26 11:00:54 +00:00
|
|
|
#include "mh/hashes.h"
|
2016-07-31 14:42:29 +00:00
|
|
|
|
2016-07-30 10:39:26 +00:00
|
|
|
#include "mh/errors.h"
|
2016-08-10 13:46:15 +00:00
|
|
|
#include "mh/generic.h"
|
2016-07-31 14:42:29 +00:00
|
|
|
|
|
|
|
|
2016-07-31 18:31:19 +00:00
|
|
|
static const struct hash_info {
|
2016-07-31 14:42:29 +00:00
|
|
|
int hash;
|
|
|
|
const char *name;
|
2016-07-31 18:31:19 +00:00
|
|
|
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 }
|
2016-07-31 14:42:29 +00:00
|
|
|
};
|
|
|
|
|
2016-07-31 18:31:19 +00:00
|
|
|
mh_assert_static(sizeof(hash_infos) / sizeof(hash_infos[0]) == MH_H_COUNT);
|
2016-07-31 14:42:29 +00:00
|
|
|
|
2016-07-31 18:31:19 +00:00
|
|
|
// Searches for given hash in hash info table
|
|
|
|
static const struct hash_info *find_hash(int hash) {
|
|
|
|
// naive search, could be replaced with binary
|
2016-07-31 14:42:29 +00:00
|
|
|
unsigned int i = 0;
|
2016-07-31 18:31:19 +00:00
|
|
|
for (; i < MH_H_COUNT; i++) {
|
|
|
|
if (hash_infos[i].hash == hash)
|
|
|
|
return &hash_infos[i];
|
2016-07-31 14:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-07-26 15:53:52 +00:00
|
|
|
|
2016-07-31 18:31:19 +00:00
|
|
|
const char *mh_hash_name(int hash) {
|
|
|
|
const struct hash_info *info = find_hash(hash);
|
|
|
|
return info ? info->name : NULL;
|
|
|
|
}
|
2016-07-26 15:53:52 +00:00
|
|
|
|
|
|
|
|
2016-07-31 14:42:29 +00:00
|
|
|
int mh_hash_default_length(int hash) {
|
2016-07-31 18:31:19 +00:00
|
|
|
const struct hash_info *info = find_hash(hash);
|
|
|
|
return info ? info->length : MH_E_UNKNOWN_CODE;
|
2016-07-26 15:53:52 +00:00
|
|
|
}
|