hash.c 3.15 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8
 */

#include "hash.h"
9

10 11
int git_hash_global_init(void)
{
12 13 14 15 16
	if (git_hash_sha1_global_init() < 0 ||
	    git_hash_sha256_global_init() < 0)
		return -1;

	return 0;
17 18
}

19
int git_hash_ctx_init(git_hash_ctx *ctx, git_hash_algorithm_t algorithm)
20
{
21 22
	int error;

23 24 25 26
	switch (algorithm) {
	case GIT_HASH_ALGORITHM_SHA1:
		error = git_hash_sha1_ctx_init(&ctx->ctx.sha1);
		break;
27 28 29
	case GIT_HASH_ALGORITHM_SHA256:
		error = git_hash_sha256_ctx_init(&ctx->ctx.sha256);
		break;
30 31 32 33
	default:
		git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
		error = -1;
	}
34

35 36
	ctx->algorithm = algorithm;
	return error;
37 38 39 40
}

void git_hash_ctx_cleanup(git_hash_ctx *ctx)
{
41
	switch (ctx->algorithm) {
42 43 44
	case GIT_HASH_ALGORITHM_SHA1:
		git_hash_sha1_ctx_cleanup(&ctx->ctx.sha1);
		return;
45 46 47
	case GIT_HASH_ALGORITHM_SHA256:
		git_hash_sha256_ctx_cleanup(&ctx->ctx.sha256);
		return;
48 49
	default:
		/* unreachable */ ;
50
	}
51 52
}

53
int git_hash_init(git_hash_ctx *ctx)
54
{
55
	switch (ctx->algorithm) {
56 57
	case GIT_HASH_ALGORITHM_SHA1:
		return git_hash_sha1_init(&ctx->ctx.sha1);
58 59
	case GIT_HASH_ALGORITHM_SHA256:
		return git_hash_sha256_init(&ctx->ctx.sha256);
60 61
	default:
		/* unreachable */ ;
62
	}
63 64

	git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
65
	return -1;
66 67
}

68
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
69
{
70
	switch (ctx->algorithm) {
71 72
	case GIT_HASH_ALGORITHM_SHA1:
		return git_hash_sha1_update(&ctx->ctx.sha1, data, len);
73 74
	case GIT_HASH_ALGORITHM_SHA256:
		return git_hash_sha256_update(&ctx->ctx.sha256, data, len);
75 76
	default:
		/* unreachable */ ;
77
	}
78 79

	git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
80
	return -1;
81 82
}

83
int git_hash_final(unsigned char *out, git_hash_ctx *ctx)
84
{
85
	switch (ctx->algorithm) {
86 87
	case GIT_HASH_ALGORITHM_SHA1:
		return git_hash_sha1_final(out, &ctx->ctx.sha1);
88 89
	case GIT_HASH_ALGORITHM_SHA256:
		return git_hash_sha256_final(out, &ctx->ctx.sha256);
90 91
	default:
		/* unreachable */ ;
92
	}
93 94

	git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
95
	return -1;
96 97
}

98
int git_hash_buf(
99
	unsigned char *out,
100 101 102
	const void *data,
	size_t len,
	git_hash_algorithm_t algorithm)
103
{
104
	git_hash_ctx ctx;
105
	int error = 0;
106

107
	if (git_hash_ctx_init(&ctx, algorithm) < 0)
108
		return -1;
109

110 111
	if ((error = git_hash_update(&ctx, data, len)) >= 0)
		error = git_hash_final(out, &ctx);
112

113
	git_hash_ctx_cleanup(&ctx);
Edward Thomson committed
114

115
	return error;
116 117
}

118
int git_hash_vec(
119
	unsigned char *out,
120
	git_str_vec *vec,
121 122
	size_t n,
	git_hash_algorithm_t algorithm)
123
{
124
	git_hash_ctx ctx;
125 126
	size_t i;
	int error = 0;
127

128
	if (git_hash_ctx_init(&ctx, algorithm) < 0)
129
		return -1;
130

131
	for (i = 0; i < n; i++) {
132
		if ((error = git_hash_update(&ctx, vec[i].data, vec[i].len)) < 0)
133 134
			goto done;
	}
135

136
	error = git_hash_final(out, &ctx);
137

138
done:
139
	git_hash_ctx_cleanup(&ctx);
140

141
	return error;
142
}
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

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;
}