hash.c 902 Bytes
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 9
 */

#include "common.h"
#include "hash.h"
10

11
int git_hash_buf(git_oid *out, const void *data, size_t len)
12
{
13
	git_hash_ctx ctx;
14
	int error = 0;
15

16
	if (git_hash_ctx_init(&ctx) < 0)
17
		return -1;
18

19 20
	if ((error = git_hash_update(&ctx, data, len)) >= 0)
		error = git_hash_final(out, &ctx);
21

22
	git_hash_ctx_cleanup(&ctx);
23 24
	
	return error;
25 26
}

27
int git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n)
28
{
29
	git_hash_ctx ctx;
30 31
	size_t i;
	int error = 0;
32

33
	if (git_hash_ctx_init(&ctx) < 0)
34
		return -1;
35

36
	for (i = 0; i < n; i++) {
37
		if ((error = git_hash_update(&ctx, vec[i].data, vec[i].len)) < 0)
38 39
			goto done;
	}
40

41
	error = git_hash_final(out, &ctx);
42

43
done:
44
	git_hash_ctx_cleanup(&ctx);
45

46
	return error;
47
}