1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 09:32:44 +01:00

sys/vfs_util: add vfs_file_sha256()

This commit is contained in:
Benjamin Valentin 2022-04-30 18:14:20 +02:00
parent 846adfc0cc
commit 427d06c987
2 changed files with 40 additions and 0 deletions

View File

@ -83,6 +83,22 @@ int vfs_file_md5(const char* file, void *digest,
*/
int vfs_file_sha1(const char* file, void *digest,
void *work_buf, size_t work_buf_len);
/**
* @brief Compute the SHA256 message digest of a file
*
* Requires the `hashes` module.
*
* @param[in] file Source file path
* @param[out] digest Destination buffer, must fit @ref SHA256_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_sha256(const char* file, void *digest,
void *work_buf, size_t work_buf_len);
#endif
#ifdef __cplusplus

View File

@ -83,6 +83,7 @@ int vfs_file_to_buffer(const char* file, void* buf, size_t len)
#if MODULE_HASHES
#include "hashes/md5.h"
#include "hashes/sha1.h"
#include "hashes/sha256.h"
int vfs_file_md5(const char* file, void *digest,
void *work_buf, size_t work_buf_len)
@ -128,5 +129,28 @@ int vfs_file_sha1(const char* file, void *digest,
return res > 0 ? 0 : res;
}
int vfs_file_sha256(const char* file, void *digest,
void *work_buf, size_t work_buf_len)
{
sha256_context_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;
}
sha256_init(&ctx);
while ((res = vfs_read(fd, work_buf, work_buf_len)) > 0) {
sha256_update(&ctx, work_buf, res);
}
sha256_final(&ctx, digest);
vfs_close(fd);
return res > 0 ? 0 : res;
}
#endif /* MODULE_HASHES */