]> git.proxmox.com Git - libgit2.git/blame - src/hash.c
Merge pull request #392 from sschuberth/development
[libgit2.git] / src / hash.c
CommitLineData
007e0753 1/*
bb742ede 2 * Copyright (C) 2009-2011 the libgit2 contributors
007e0753 3 *
bb742ede
VM
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
007e0753
RJ
6 */
7
8#include "common.h"
9#include "hash.h"
d03f5675
RJ
10
11#if defined(PPC_SHA1)
12# include "ppc/sha1.h"
d03f5675 13#else
17d52304 14# include "sha1.h"
d03f5675 15#endif
007e0753
RJ
16
17struct git_hash_ctx {
38c513b9 18 SHA_CTX c;
007e0753
RJ
19};
20
21git_hash_ctx *git_hash_new_ctx(void)
22{
64a47c01 23 git_hash_ctx *ctx = git__malloc(sizeof(*ctx));
007e0753
RJ
24
25 if (!ctx)
26 return NULL;
27
38c513b9 28 SHA1_Init(&ctx->c);
007e0753
RJ
29
30 return ctx;
31}
32
33void git_hash_free_ctx(git_hash_ctx *ctx)
34{
35 free(ctx);
36}
37
38void git_hash_init(git_hash_ctx *ctx)
39{
40 assert(ctx);
38c513b9 41 SHA1_Init(&ctx->c);
007e0753
RJ
42}
43
44void git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
45{
46 assert(ctx);
38c513b9 47 SHA1_Update(&ctx->c, data, len);
007e0753
RJ
48}
49
50void git_hash_final(git_oid *out, git_hash_ctx *ctx)
51{
52 assert(ctx);
38c513b9 53 SHA1_Final(out->id, &ctx->c);
007e0753
RJ
54}
55
56void git_hash_buf(git_oid *out, const void *data, size_t len)
57{
38c513b9 58 SHA_CTX c;
007e0753 59
38c513b9
RJ
60 SHA1_Init(&c);
61 SHA1_Update(&c, data, len);
62 SHA1_Final(out->id, &c);
007e0753
RJ
63}
64
65void git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n)
66{
38c513b9 67 SHA_CTX c;
007e0753
RJ
68 size_t i;
69
38c513b9 70 SHA1_Init(&c);
007e0753 71 for (i = 0; i < n; i++)
38c513b9
RJ
72 SHA1_Update(&c, vec[i].data, vec[i].len);
73 SHA1_Final(out->id, &c);
007e0753 74}