commit e9ba3467c2b7dd17437afd611e1f2c98da726750 from: Omar Polo date: Sat Feb 04 13:58:02 2023 UTC provide functions to parse/serialize sha256 digests commit - 69c6accf47680d70def917cbab0e761177d8264b commit + e9ba3467c2b7dd17437afd611e1f2c98da726750 blob - 141cb2590fa14834c63381929a0a7438ae3e4556 blob + faf9e8a717096264c717436f464e9b6cbf22d64e --- lib/got_lib_sha1.h +++ lib/got_lib_sha1.h @@ -15,7 +15,12 @@ */ #define GOT_SHA1_STRING_ZERO "0000000000000000000000000000000000000000" +#define GOT_SHA256_STRING_ZERO "0000000000000000000000000000000000000000000000000000000000000000" int got_parse_xdigit(uint8_t *, const char *); + int got_parse_sha1_digest(uint8_t *, const char *); char *got_sha1_digest_to_str(const uint8_t *, char *, size_t); + +int got_parse_sha256_digest(uint8_t *, const char *); +char *got_sha256_digest_to_str(const uint8_t *, char *, size_t); blob - ee835ad4c90671b4fd6fd88951b7b5c716f05bce blob + 78088ef7e3d68c1934326f353ffd25066d280bfe --- lib/sha1.c +++ lib/sha1.c @@ -83,3 +83,46 @@ got_sha1_digest_to_str(const uint8_t *digest, char *bu return buf; } + +int +got_parse_sha256_digest(uint8_t *digest, const char *line) +{ + uint8_t b = 0; + char hex[3] = {'\0', '\0', '\0'}; + int i, j; + + for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { + if (line[0] == '\0' || line[1] == '\0') + return 0; + for (j = 0; j < 2; j++) { + hex[j] = *line; + line++; + } + if (!got_parse_xdigit(&b, hex)) + return 0; + digest[i] = b; + } + + return 1; +} + +char * +got_sha256_digest_to_str(const uint8_t *digest, char *buf, size_t size) +{ + char *p = buf; + char hex[3]; + int i; + + if (size < SHA256_DIGEST_STRING_LENGTH) + return NULL; + + for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { + snprintf(hex, sizeof(hex), "%.2x", digest[i]); + p[0] = hex[0]; + p[1] = hex[1]; + p += 2; + } + p[0] = '\0'; + + return buf; +}