]> git.proxmox.com Git - libgit2.git/blob - src/hash.c
Merge pull request #321 from letolabs/readme
[libgit2.git] / src / hash.c
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26 #include "common.h"
27 #include "hash.h"
28
29 #if defined(PPC_SHA1)
30 # include "ppc/sha1.h"
31 #else
32 # include "sha1.h"
33 #endif
34
35 struct git_hash_ctx {
36 SHA_CTX c;
37 };
38
39 git_hash_ctx *git_hash_new_ctx(void)
40 {
41 git_hash_ctx *ctx = git__malloc(sizeof(*ctx));
42
43 if (!ctx)
44 return NULL;
45
46 SHA1_Init(&ctx->c);
47
48 return ctx;
49 }
50
51 void git_hash_free_ctx(git_hash_ctx *ctx)
52 {
53 free(ctx);
54 }
55
56 void git_hash_init(git_hash_ctx *ctx)
57 {
58 assert(ctx);
59 SHA1_Init(&ctx->c);
60 }
61
62 void git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
63 {
64 assert(ctx);
65 SHA1_Update(&ctx->c, data, len);
66 }
67
68 void git_hash_final(git_oid *out, git_hash_ctx *ctx)
69 {
70 assert(ctx);
71 SHA1_Final(out->id, &ctx->c);
72 }
73
74 void git_hash_buf(git_oid *out, const void *data, size_t len)
75 {
76 SHA_CTX c;
77
78 SHA1_Init(&c);
79 SHA1_Update(&c, data, len);
80 SHA1_Final(out->id, &c);
81 }
82
83 void git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n)
84 {
85 SHA_CTX c;
86 size_t i;
87
88 SHA1_Init(&c);
89 for (i = 0; i < n; i++)
90 SHA1_Update(&c, vec[i].data, vec[i].len);
91 SHA1_Final(out->id, &c);
92 }