]> git.proxmox.com Git - libgit2.git/blob - src/hash.c
install as examples
[libgit2.git] / src / hash.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8 #include "hash.h"
9
10 int git_hash_global_init(void)
11 {
12 return git_hash_sha1_global_init();
13 }
14
15 int git_hash_ctx_init(git_hash_ctx *ctx)
16 {
17 int error;
18
19 if ((error = git_hash_sha1_ctx_init(&ctx->sha1)) < 0)
20 return error;
21
22 ctx->algo = GIT_HASH_ALGO_SHA1;
23
24 return 0;
25 }
26
27 void git_hash_ctx_cleanup(git_hash_ctx *ctx)
28 {
29 switch (ctx->algo) {
30 case GIT_HASH_ALGO_SHA1:
31 git_hash_sha1_ctx_cleanup(&ctx->sha1);
32 return;
33 default:
34 assert(0);
35 }
36 }
37
38 int git_hash_init(git_hash_ctx *ctx)
39 {
40 switch (ctx->algo) {
41 case GIT_HASH_ALGO_SHA1:
42 return git_hash_sha1_init(&ctx->sha1);
43 default:
44 assert(0);
45 return -1;
46 }
47 }
48
49 int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
50 {
51 switch (ctx->algo) {
52 case GIT_HASH_ALGO_SHA1:
53 return git_hash_sha1_update(&ctx->sha1, data, len);
54 default:
55 assert(0);
56 return -1;
57 }
58 }
59
60 int git_hash_final(git_oid *out, git_hash_ctx *ctx)
61 {
62 switch (ctx->algo) {
63 case GIT_HASH_ALGO_SHA1:
64 return git_hash_sha1_final(out, &ctx->sha1);
65 default:
66 assert(0);
67 return -1;
68 }
69 }
70
71 int git_hash_buf(git_oid *out, const void *data, size_t len)
72 {
73 git_hash_ctx ctx;
74 int error = 0;
75
76 if (git_hash_ctx_init(&ctx) < 0)
77 return -1;
78
79 if ((error = git_hash_update(&ctx, data, len)) >= 0)
80 error = git_hash_final(out, &ctx);
81
82 git_hash_ctx_cleanup(&ctx);
83
84 return error;
85 }
86
87 int git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n)
88 {
89 git_hash_ctx ctx;
90 size_t i;
91 int error = 0;
92
93 if (git_hash_ctx_init(&ctx) < 0)
94 return -1;
95
96 for (i = 0; i < n; i++) {
97 if ((error = git_hash_update(&ctx, vec[i].data, vec[i].len)) < 0)
98 goto done;
99 }
100
101 error = git_hash_final(out, &ctx);
102
103 done:
104 git_hash_ctx_cleanup(&ctx);
105
106 return error;
107 }