Skip to content

Commit

Permalink
[util] Add a binary to hex string convertor
Browse files Browse the repository at this point in the history
The Issuer and Subject are both tstr (text-string) type.
Using key_id as issuer/subject requires a conventor.

Bug: 356532759
Signed-off-by: Tommy Chiu <tommychiu@google.com>
  • Loading branch information
tommychiu-github authored and timothytrippel committed Oct 23, 2024
1 parent 768baf2 commit 6b4f055
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions sw/device/silicon_creator/lib/base/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ void util_reverse_bytes(void *buf, size_t num_bytes) {
byte_buf[num_bytes - i - 1] = temp;
}
}

// Returns hexdump character for the half-byte.
static inline uint8_t hexdump_halfbyte(uint8_t half_byte) {
if (half_byte < 10)
return '0' + half_byte;
else
return 'a' + half_byte - 10;
}

void util_hexdump_byte(uint8_t byte, uint8_t *str) {
str[0] = hexdump_halfbyte((byte & 0xF0) >> 4);
str[1] = hexdump_halfbyte(byte & 0x0F);
}
9 changes: 9 additions & 0 deletions sw/device/silicon_creator/lib/base/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ uint32_t util_size_to_words(uint32_t bytes);
*/
void util_reverse_bytes(void *buf, size_t num_bytes);

/**
* Fills hexdump of the byte (lowercase).
*
* @param byte Byte to convert to a hex string.
* @param[out] str String buffer (always 2 bytes) to place hex string encoded
* byte in.
*/
void util_hexdump_byte(uint8_t byte, uint8_t *str);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 6b4f055

Please sign in to comment.