start implementing default lengths
fixup syle in few places
This commit is contained in:
parent
0eafc13839
commit
b154a83b35
6 changed files with 89 additions and 11 deletions
5
Makefile
5
Makefile
|
@ -44,7 +44,7 @@ test: $(TEST_BINS)
|
|||
@for t in $^; do \
|
||||
echo; \
|
||||
echo '***' "$$t.c" '***'; \
|
||||
"./$$t"; \
|
||||
"./$$t" || printf "\n!!! TEST FAILURE !!!\n"; \
|
||||
done
|
||||
|
||||
tests/c/test_%.o: tests/c/test_%.c
|
||||
|
@ -66,4 +66,7 @@ clean:
|
|||
|
||||
dist-clean: clean
|
||||
|
||||
PHONY += build
|
||||
build: all
|
||||
|
||||
.PHONY: $(PHONY)
|
||||
|
|
|
@ -15,11 +15,12 @@ typedef enum {
|
|||
MH_H_BLAKE2B,
|
||||
MH_H_BLAKE2S,
|
||||
|
||||
HM_H_COUNT // number of hash functions
|
||||
MH_H_COUNT // number of hash functions
|
||||
} mh_hash;
|
||||
|
||||
// returns default size in bytes of given hash function
|
||||
int mh_hash_default_size(mh_hash hash);
|
||||
// returns default length in bytes of given hash function
|
||||
// or -1 in case of invalid arguemnt
|
||||
int mh_hash_default_length(mh_hash hash);
|
||||
|
||||
// returns wire format code of given hash function
|
||||
int mh_hash_code(mh_hash hash);
|
||||
|
|
45
src/hashes.c
45
src/hashes.c
|
@ -1,2 +1,47 @@
|
|||
/* vim: set ts=8 sw=8 noexpandtab: */
|
||||
#include "mh/hashes.h"
|
||||
|
||||
|
||||
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
|
||||
// TODO(Kubuxu): implement blake lengths
|
||||
};
|
||||
|
||||
|
||||
int mh_hash_default_length(mh_hash hash) {
|
||||
if (hash > MH_H_SHAKE_256)
|
||||
return -2; // TODO(Kubuxu): remove it after blake is implemented
|
||||
if (hash < 0 || hash >= MH_H_COUNT)
|
||||
return -1;
|
||||
|
||||
return hash_lengths[0];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
MH_H_SHA1,
|
||||
MH_H_SHA2_256,
|
||||
MH_H_SHA2_512,
|
||||
MH_H_SHA3_512,
|
||||
MH_H_SHA3_384,
|
||||
MH_H_SHA3_256,
|
||||
MH_H_SHA3_224,
|
||||
MH_H_SHAKE_128,
|
||||
MH_H_SHAKE_256,
|
||||
MH_H_BLAKE2B,
|
||||
MH_H_BLAKE2S,
|
||||
|
||||
HM_H_COUNT // number of hash functions
|
||||
*/
|
||||
|
|
|
@ -9,8 +9,8 @@ static char *mu_all_tests(void);
|
|||
|
||||
int main(void) {
|
||||
char *result = mu_all_tests();
|
||||
if (result != 0) {
|
||||
printf("%s\n", result);
|
||||
if (result != NULL) {
|
||||
printf("error: %s\n", result);
|
||||
}
|
||||
else {
|
||||
printf("ALL TESTS PASSED\n");
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim: set ts=8 sw=8 noexpandtab: */
|
||||
#include <string.h>
|
||||
#include "minunit.h"
|
||||
|
||||
|
@ -12,10 +13,10 @@ static char *test_error_messages_exist(void) {
|
|||
mu_assert(error_buf, strlen(mh_error_string((mh_error) i)));
|
||||
}
|
||||
|
||||
return 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char *mu_all_tests(void) {
|
||||
mu_run_test(test_error_messages_exist);
|
||||
return 0;
|
||||
return NULL;
|
||||
}
|
||||
|
|
28
tests/c/test_hashes_length.c
Normal file
28
tests/c/test_hashes_length.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* vim: set ts=8 sw=8 noexpandtab: */
|
||||
#include <string.h>
|
||||
#include "minunit.h"
|
||||
|
||||
#include "mh/hashes.h"
|
||||
|
||||
char error_buf[256];
|
||||
|
||||
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);
|
||||
if (length <= 0) {
|
||||
sprintf(error_buf, "mh_hash_default_length: hash %d"
|
||||
" returned invalid (%d) default length",
|
||||
i, length);
|
||||
return error_buf;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char *mu_all_tests(void) {
|
||||
mu_run_test(test_all_hashes_have_lengths);
|
||||
return NULL;
|
||||
}
|
||||
|
Loading…
Reference in a new issue