core: add hash and rope tests
This commit is contained in:
74
core-test/hash.c
Normal file
74
core-test/hash.c
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
#include <blue/core/hash.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static void print_digest(
|
||||||
|
const char *func_name, b_hash_function func, const char *msg,
|
||||||
|
size_t msg_len, size_t digest_len)
|
||||||
|
{
|
||||||
|
unsigned char digest[128];
|
||||||
|
|
||||||
|
b_hash_ctx ctx;
|
||||||
|
if (!B_OK(b_hash_ctx_init(&ctx, func))) {
|
||||||
|
printf("b_hash_ctx_init failed\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!B_OK(b_hash_ctx_update(&ctx, msg, msg_len))) {
|
||||||
|
printf("b_hash_ctx_update failed\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!B_OK(b_hash_ctx_finish(&ctx, digest, sizeof digest))) {
|
||||||
|
printf("b_hash_ctx_finish failed\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char digest_str[256];
|
||||||
|
for (size_t i = 0; i < digest_len; i++) {
|
||||||
|
snprintf(
|
||||||
|
digest_str + (i * 2), sizeof digest_str - (i * 2),
|
||||||
|
"%02x", digest[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s(%s) = %s\n", func_name, msg, digest_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
const char *msg = "Hello, world!";
|
||||||
|
size_t msg_len = strlen(msg);
|
||||||
|
|
||||||
|
print_digest("MD4", B_HASH_MD4, msg, msg_len, B_DIGEST_LENGTH_MD4);
|
||||||
|
print_digest("MD5", B_HASH_MD5, msg, msg_len, B_DIGEST_LENGTH_MD5);
|
||||||
|
print_digest("SHA1", B_HASH_SHA1, msg, msg_len, B_DIGEST_LENGTH_SHA1);
|
||||||
|
print_digest(
|
||||||
|
"SHA224", B_HASH_SHA2_224, msg, msg_len, B_DIGEST_LENGTH_SHA2_224);
|
||||||
|
print_digest(
|
||||||
|
"SHA256", B_HASH_SHA2_256, msg, msg_len, B_DIGEST_LENGTH_SHA2_256);
|
||||||
|
print_digest(
|
||||||
|
"SHA384", B_HASH_SHA2_384, msg, msg_len, B_DIGEST_LENGTH_SHA2_384);
|
||||||
|
print_digest(
|
||||||
|
"SHA512", B_HASH_SHA2_512, msg, msg_len, B_DIGEST_LENGTH_SHA2_512);
|
||||||
|
print_digest(
|
||||||
|
"SHA3-224", B_HASH_SHA3_224, msg, msg_len,
|
||||||
|
B_DIGEST_LENGTH_SHA3_224);
|
||||||
|
print_digest(
|
||||||
|
"SHA3-256", B_HASH_SHA3_256, msg, msg_len,
|
||||||
|
B_DIGEST_LENGTH_SHA3_256);
|
||||||
|
print_digest(
|
||||||
|
"SHA3-384", B_HASH_SHA3_384, msg, msg_len,
|
||||||
|
B_DIGEST_LENGTH_SHA3_384);
|
||||||
|
print_digest(
|
||||||
|
"SHA3-512", B_HASH_SHA3_512, msg, msg_len,
|
||||||
|
B_DIGEST_LENGTH_SHA3_512);
|
||||||
|
print_digest(
|
||||||
|
"SHAKE128", B_HASH_SHAKE128, msg, msg_len,
|
||||||
|
B_DIGEST_LENGTH_SHAKE128);
|
||||||
|
print_digest(
|
||||||
|
"SHAKE256", B_HASH_SHAKE256, msg, msg_len,
|
||||||
|
B_DIGEST_LENGTH_SHAKE256);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
28
core-test/ropes.c
Normal file
28
core-test/ropes.c
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include <blue/core/rope.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
b_rope a = B_ROPE_CHAR('a');
|
||||||
|
b_rope b = B_ROPE_CSTR_STATIC("Hello, world!");
|
||||||
|
b_rope c = B_ROPE_INT(-4096);
|
||||||
|
b_rope d = B_ROPE_UINT(4096);
|
||||||
|
|
||||||
|
b_rope str;
|
||||||
|
|
||||||
|
const b_rope *ropes[] = {
|
||||||
|
&a,
|
||||||
|
&b,
|
||||||
|
&c,
|
||||||
|
&d,
|
||||||
|
};
|
||||||
|
|
||||||
|
b_rope_join(&str, ropes, sizeof ropes / sizeof ropes[0]);
|
||||||
|
|
||||||
|
char cstr[1024];
|
||||||
|
b_rope_to_cstr(&str, cstr, sizeof cstr);
|
||||||
|
b_rope_destroy(&str);
|
||||||
|
|
||||||
|
printf("%s\n", cstr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user