hash.c 1.2 KB
Newer Older
1
/*
Vicent Marti committed
2
 * Copyright (C) 2009-2011 the libgit2 contributors
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 12 13

#if defined(PPC_SHA1)
# include "ppc/sha1.h"
#else
14
# include "sha1.h"
15
#endif
16 17

struct git_hash_ctx {
18
	SHA_CTX c;
19 20 21 22
};

git_hash_ctx *git_hash_new_ctx(void)
{
23
	git_hash_ctx *ctx = git__malloc(sizeof(*ctx));
24 25 26 27

	if (!ctx)
		return NULL;

28
	SHA1_Init(&ctx->c);
29 30 31 32 33 34

	return ctx;
}

void git_hash_free_ctx(git_hash_ctx *ctx)
{
35
	git__free(ctx);
36 37 38 39 40
}

void git_hash_init(git_hash_ctx *ctx)
{
	assert(ctx);
41
	SHA1_Init(&ctx->c);
42 43 44 45 46
}

void git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
{
	assert(ctx);
47
	SHA1_Update(&ctx->c, data, len);
48 49 50 51 52
}

void git_hash_final(git_oid *out, git_hash_ctx *ctx)
{
	assert(ctx);
53
	SHA1_Final(out->id, &ctx->c);
54 55 56 57
}

void git_hash_buf(git_oid *out, const void *data, size_t len)
{
58
	SHA_CTX c;
59

60 61 62
	SHA1_Init(&c);
	SHA1_Update(&c, data, len);
	SHA1_Final(out->id, &c);
63 64 65 66
}

void git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n)
{
67
	SHA_CTX c;
68 69
	size_t i;

70
	SHA1_Init(&c);
71
	for (i = 0; i < n; i++)
72 73
		SHA1_Update(&c, vec[i].data, vec[i].len);
	SHA1_Final(out->id, &c);
74
}