Skip to content

Commit 0e53e55

Browse files
committed
hash: introduce git_hash_fmt
A simple hash-to-hexadigit formatter.
1 parent a9fc14b commit 0e53e55

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/hash.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,19 @@ int git_hash_vec(
124124

125125
return error;
126126
}
127+
128+
int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len)
129+
{
130+
static char hex[] = "0123456789abcdef";
131+
char *str = out;
132+
size_t i;
133+
134+
for (i = 0; i < hash_len; i++) {
135+
*str++ = hex[hash[i] >> 4];
136+
*str++ = hex[hash[i] & 0x0f];
137+
}
138+
139+
*str++ = '\0';
140+
141+
return 0;
142+
}

src/hash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ int git_hash_final(unsigned char *out, git_hash_ctx *c);
4141
int git_hash_buf(unsigned char *out, const void *data, size_t len, git_hash_algorithm_t algorithm);
4242
int git_hash_vec(unsigned char *out, git_str_vec *vec, size_t n, git_hash_algorithm_t algorithm);
4343

44+
int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len);
45+
4446
#endif

0 commit comments

Comments
 (0)