From f60bea0fdd7067d1645a805e05cc4a6f4ceffa7a Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Thu, 28 Mar 2024 19:09:39 -0700 Subject: [PATCH] add hex_encode --- docs/crypto.md | 20 ++++++++++++++++++-- include/lfcrypto.h | 3 ++- src/crypto.c | 18 ++++++++++++++++-- tests/tests.c | 4 ++++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/docs/crypto.md b/docs/crypto.md index 7b6c419..33714eb 100644 --- a/docs/crypto.md +++ b/docs/crypto.md @@ -23,16 +23,32 @@ in case you need to know the size of the decoded data, otherwise you can just pa unsigned char *b64_decode(const char *s, size_t sz, size_t *decode_sz); ``` +### hex_encode + +Encodes an array of bytes into a string in hex representation. For example, converts +`[0xDE, 0xAD, 0xBE, 0xEF]` into `"DEADBEEF"`. The returned string will always be in capital +letters with no leading `0x`. User is responsible for the freeing of the returned string + +```c +const char *hex_encode(const unsigned char *hex, size_t sz); + +// Example +const char *s = hex_encode(h, 4); +assert(strcmp(s, "DEADBEEF") == 0); +free(s); +``` + ### hex_decode Decodes a string of characters representing hexadecimal bytes into an array of `unsigned char`. -For example, converts `"DEADBEEF"` into `[0xDE, 0xAD, 0xBE, 0xEF]`. +For example, converts `"DEADBEEF"` into `[0xDE, 0xAD, 0xBE, 0xEF]`. User is reponsible for the +freeing of the returned byte array ```c unsigned char *hex_decode(const char *orig, size_t *sz); // Example -s = hex_decode("DEADBEEF", &s_sz); +unsigned char *s = hex_decode("DEADBEEF", &s_sz); unsigned char h[4] = { 0xDE, 0xAD, 0xBE, 0xEF }; diff --git a/include/lfcrypto.h b/include/lfcrypto.h index 7a07405..0771811 100644 --- a/include/lfcrypto.h +++ b/include/lfcrypto.h @@ -3,8 +3,9 @@ #include -unsigned char *b64_encode(const unsigned char *s, size_t sz); +const char *b64_encode(const unsigned char *s, size_t sz); unsigned char *b64_decode(const char *s, size_t sz, size_t *decode_sz); +const char *hex_encode(const unsigned char *hex, size_t sz); unsigned char *hex_decode(const char *orig, size_t *sz); #endif // LIBFLINT_CRYPTO_H diff --git a/src/crypto.c b/src/crypto.c index 4795e3d..0286f8c 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -45,7 +45,7 @@ static int resize_b64_buf(b64_buf *b, size_t sz) { return 0; } -unsigned char *b64_encode(const unsigned char *s, size_t sz) { +const char *b64_encode(const unsigned char *s, size_t sz) { int i = 0; b64_buf encbuf; size_t size = 0; @@ -110,7 +110,7 @@ unsigned char *b64_encode(const unsigned char *s, size_t sz) { } encbuf.ptr[size] = '\0'; - return encbuf.ptr; + return (char *)encbuf.ptr; } unsigned char *b64_decode(const char *s, size_t sz, size_t *decode_sz) { @@ -229,3 +229,17 @@ unsigned char *hex_decode(const char *orig, size_t *sz) { free(buf); return hex; } + +const char *hex_encode(const unsigned char *hex, size_t sz) { + size_t ssz = sz * 2 + 1; + char *s = malloc(sizeof(char) * ssz); + char *pos = s; + + for (size_t i = 0; i < sz; ++i) { + snprintf(pos, 3, "%2X", hex[i]); + pos += 2; + } + s[ssz - 1] = '\0'; + + return s; +} diff --git a/tests/tests.c b/tests/tests.c index 142b87a..5193efa 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -345,6 +345,10 @@ void test_crypto() { assert(s[i] == h2[i]); } free(s); + + s = hex_encode(h, 4); + assert(strcmp(s, "DEADBEEF") == 0); + free(s); } #if defined(__APPLE__) || defined(__MACH__)