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_sha1()

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

View File

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

View File

@ -82,6 +82,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"
int vfs_file_md5(const char* file, void *digest,
void *work_buf, size_t work_buf_len)
@ -105,4 +106,27 @@ int vfs_file_md5(const char* file, void *digest,
return res > 0 ? 0 : res;
}
int vfs_file_sha1(const char* file, void *digest,
void *work_buf, size_t work_buf_len)
{
sha1_context ctx;
int res, fd = vfs_open(file, O_RDONLY, 0);
if (fd < 0) {
DEBUG("can't open %s for reading\n", file);
return fd;
}
sha1_init(&ctx);
while ((res = vfs_read(fd, work_buf, work_buf_len)) > 0) {
sha1_update(&ctx, work_buf, res);
}
sha1_final(&ctx, digest);
vfs_close(fd);
return res > 0 ? 0 : res;
}
#endif /* MODULE_HASHES */