1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/vfs_util: add vfs_file_md5()

This commit is contained in:
Benjamin Valentin 2022-04-30 18:02:06 +02:00
parent 025af7534f
commit 65578269fc
2 changed files with 45 additions and 0 deletions

View File

@ -51,6 +51,24 @@ int vfs_file_from_buffer(const char *file, const void *buf, size_t len);
*/
int vfs_file_to_buffer(const char* file, void* buf, size_t len);
#if MODULE_HASHES || DOXYGEN
/**
* @brief Compute the MD5 message digest of a file
*
* Requires the `hashes` module.
*
* @param[in] file Source file path
* @param[out] digest Destination buffer, must fit @ref MD5_DIGEST_LENGTH bytes
* @param[out] work_buf Work buffer
* @param[in] work_buf_len Size of the work buffer
*
* @return 0 on success
* @return negative error
*/
int vfs_file_md5(const char* file, void *digest,
void *work_buf, size_t work_buf_len);
#endif
#ifdef __cplusplus
}
#endif

View File

@ -79,3 +79,30 @@ int vfs_file_to_buffer(const char* file, void* buf, size_t len)
return res;
}
#if MODULE_HASHES
#include "hashes/md5.h"
int vfs_file_md5(const char* file, void *digest,
void *work_buf, size_t work_buf_len)
{
md5_ctx_t ctx;
int res, fd = vfs_open(file, O_RDONLY, 0);
if (fd < 0) {
DEBUG("can't open %s for reading\n", file);
return fd;
}
md5_init(&ctx);
while ((res = vfs_read(fd, work_buf, work_buf_len)) > 0) {
md5_update(&ctx, work_buf, res);
}
md5_final(&ctx, digest);
vfs_close(fd);
return res > 0 ? 0 : res;
}
#endif /* MODULE_HASHES */