Commit 0e53e55d7b400e303caf271469f584a7cf92036b

Edward Thomson 2022-01-22T14:18:48

hash: introduce git_hash_fmt A simple hash-to-hexadigit formatter.

diff --git a/src/hash.c b/src/hash.c
index 92e7ff2..98ceb05 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -124,3 +124,19 @@ done:
 
 	return error;
 }
+
+int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len)
+{
+	static char hex[] = "0123456789abcdef";
+	char *str = out;
+	size_t i;
+
+	for (i = 0; i < hash_len; i++) {
+		*str++ = hex[hash[i] >> 4];
+		*str++ = hex[hash[i] & 0x0f];
+	}
+
+	*str++ = '\0';
+
+	return 0;
+}
diff --git a/src/hash.h b/src/hash.h
index 2b2eaf6..507c1cb 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -41,4 +41,6 @@ int git_hash_final(unsigned char *out, git_hash_ctx *c);
 int git_hash_buf(unsigned char *out, const void *data, size_t len, git_hash_algorithm_t algorithm);
 int git_hash_vec(unsigned char *out, git_str_vec *vec, size_t n, git_hash_algorithm_t algorithm);
 
+int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len);
+
 #endif