Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sha: add sha256_printf() #725

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/re_sha.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@

void sha1(const uint8_t *d, size_t n, uint8_t *md);
void sha256(const uint8_t *d, size_t n, uint8_t *md);
int sha256_printf(uint8_t md[32], const char *fmt, ...);
30 changes: 30 additions & 0 deletions src/sha/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include <re_types.h>
#include <re_mbuf.h>
#ifdef USE_OPENSSL
#include <openssl/sha.h>
#elif defined (__APPLE__)
Expand Down Expand Up @@ -56,3 +57,32 @@ void sha256(const uint8_t *d, size_t n, uint8_t *md)
#error missing SHA-256 backend
#endif
}


/**
* Calculate the SHA-256 hash from a formatted string
*
* @param md Calculated SHA-256 hash
* @param fmt Formatted string
*
* @return 0 if success, otherwise errorcode
*/
int sha256_printf(uint8_t md[32], const char *fmt, ...)
{
struct mbuf mb;
va_list ap;
int err;

mbuf_init(&mb);

va_start(ap, fmt);
err = mbuf_vprintf(&mb, fmt, ap);
va_end(ap);

if (!err)
sha256(mb.buf, mb.end, md);

mbuf_reset(&mb);

return err;
}