feat(util): add md5_compute()

This commit is contained in:
decafcode 2023-12-08 12:34:43 -05:00 committed by decafcode
parent 59d0750be1
commit 4e8bf46ebf
5 changed files with 92 additions and 0 deletions

View File

@ -1,6 +1,7 @@
project('saltytools', 'c', version: '0.1.0')
libpng_dep = dependency('libpng', fallback: ['libpng', 'libpng_dep'])
openssl_dep = dependency('openssl', fallback: ['openssl', 'openssl_dep'])
inc = include_directories('.')

15
subprojects/openssl.wrap Normal file
View File

@ -0,0 +1,15 @@
[wrap-file]
directory = openssl-3.0.8
source_url = https://www.openssl.org/source/openssl-3.0.8.tar.gz
source_filename = openssl-3.0.8.tar.gz
source_hash = 6c13d2bf38fdf31eac3ce2a347073673f5d63263398f1f69d0df4a41253e4b3e
patch_filename = openssl_3.0.8-2_patch.zip
patch_url = https://wrapdb.mesonbuild.com/v2/openssl_3.0.8-2/get_patch
patch_hash = e84b5fe469e681e3318184157a0c7c43d4cbacd078bb88f506e31569f8f75072
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/openssl_3.0.8-2/openssl-3.0.8.tar.gz
wrapdb_version = 3.0.8-2
[provide]
libcrypto = libcrypto_dep
libssl = libssl_dep
openssl = openssl_dep

63
util/crypto.c Normal file
View File

@ -0,0 +1,63 @@
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <openssl/evp.h>
#include "util/crypto.h"
#include "util/log.h"
int md5_compute(struct md5_hash *dest, const void *bytes, size_t nbytes) {
EVP_MD_CTX *ctx;
const EVP_MD *md5;
unsigned int len;
int libr;
int r;
assert(dest != NULL);
assert(bytes != NULL);
ctx = EVP_MD_CTX_new();
if (ctx == NULL) {
r = -ENOMEM;
goto end;
}
md5 = EVP_md5();
libr = EVP_DigestInit_ex(ctx, md5, NULL);
if (libr != 1) {
r = -ENOSYS;
log_error(r);
goto end;
}
libr = EVP_DigestUpdate(ctx, bytes, nbytes);
if (libr != 1) {
r = -ENOSYS;
log_error(r);
goto end;
}
len = sizeof(dest->b);
libr = EVP_DigestFinal_ex(ctx, dest->b, &len);
if (libr != 1 || len != sizeof(dest->b)) {
r = -ENOSYS;
log_error(r);
goto end;
}
r = 0;
end:
EVP_MD_CTX_free(ctx);
return r;
}

10
util/crypto.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
struct md5_hash {
uint8_t b[16];
};
int md5_compute(struct md5_hash *dest, const void *bytes, size_t nbytes);

View File

@ -1,8 +1,11 @@
util_lib = static_library(
'util',
dependencies: [openssl_dep],
include_directories: [inc],
c_pch: '../precompiled.h',
sources: [
'crypto.c',
'crypto.h',
'fs.c',
'fs.h',
'iobuf.c',