Last active
November 25, 2021 11:31
-
-
Save leiless/53ba95b20b6552a532f73a1823481628 to your computer and use it in GitHub Desktop.
ISA-L Crypto MD5 submit & flush example (a single hash job)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdint.h> | |
#include <assert.h> | |
#include <isa-l_crypto.h> | |
#include <endian.h> | |
static MD5_HASH_CTX * | |
submit_flush(MD5_HASH_CTX_MGR *mgr, MD5_HASH_CTX *ctx, const void *buffer, HASH_CTX_FLAG flags) { | |
uint32_t len = strlen((char *) buffer); | |
MD5_HASH_CTX *job = md5_ctx_mgr_submit(mgr, ctx, buffer, len, flags); | |
if (job != NULL) { | |
assert(job == ctx); | |
assert(job->error == HASH_CTX_ERROR_NONE); | |
} | |
job = md5_ctx_mgr_flush(mgr); | |
if (job != NULL) { | |
assert(job == ctx); | |
assert(job->error == HASH_CTX_ERROR_NONE); | |
} | |
return job; | |
} | |
static void print_md5(MD5_HASH_CTX *job) { | |
const uint32_t *p = job->job.result_digest; | |
printf("%x%x%x%x\n", | |
htobe32(p[0]), | |
htobe32(p[1]), | |
htobe32(p[2]), | |
htobe32(p[3]) | |
); | |
} | |
int main(void) { | |
printf("ISA-L crypto version: %u.%u.%u\n", | |
ISAL_CRYPTO_MAJOR_VERSION, ISAL_CRYPTO_MINOR_VERSION, ISAL_CRYPTO_PATCH_VERSION); | |
MD5_HASH_CTX_MGR *mgr = NULL; | |
int e = posix_memalign((void *) &mgr, 16, sizeof *mgr); | |
assert(e == 0); | |
assert(mgr != NULL); | |
md5_ctx_mgr_init(mgr); | |
MD5_HASH_CTX ctx; | |
hash_ctx_init(&ctx); | |
(void) submit_flush(mgr, &ctx, "a", HASH_FIRST); | |
(void) submit_flush(mgr, &ctx, "b", HASH_UPDATE); | |
(void) submit_flush(mgr, &ctx, "c", HASH_LAST); | |
assert(ctx.error == HASH_CTX_ERROR_NONE); | |
print_md5(&ctx); | |
free(mgr); | |
return 0; | |
} |
Author
leiless
commented
Nov 25, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment