]> git.proxmox.com Git - libgit2.git/blob - src/hash.c
New upstream version 1.4.3+dfsg.1
[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, git_hash_algorithm_t algorithm)
16 {
17 int error;
18
19 switch (algorithm) {
20 case GIT_HASH_ALGORITHM_SHA1:
21 error = git_hash_sha1_ctx_init(&ctx->ctx.sha1);
22 break;
23 default:
24 git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
25 error = -1;
26 }
27
28 ctx->algorithm = algorithm;
29 return error;
30 }
31
32 void git_hash_ctx_cleanup(git_hash_ctx *ctx)
33 {
34 switch (ctx->algorithm) {
35 case GIT_HASH_ALGORITHM_SHA1:
36 git_hash_sha1_ctx_cleanup(&ctx->ctx.sha1);
37 return;
38 default:
39 /* unreachable */ ;
40 }
41 }
42
43 int git_hash_init(git_hash_ctx *ctx)
44 {
45 switch (ctx->algorithm) {
46 case GIT_HASH_ALGORITHM_SHA1:
47 return git_hash_sha1_init(&ctx->ctx.sha1);
48 default:
49 /* unreachable */ ;
50 }
51
52 git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
53 return -1;
54 }
55
56 int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
57 {
58 switch (ctx->algorithm) {
59 case GIT_HASH_ALGORITHM_SHA1:
60 return git_hash_sha1_update(&ctx->ctx.sha1, data, len);
61 default:
62 /* unreachable */ ;
63 }
64
65 git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
66 return -1;
67 }
68
69 int git_hash_final(unsigned char *out, git_hash_ctx *ctx)
70 {
71 switch (ctx->algorithm) {
72 case GIT_HASH_ALGORITHM_SHA1:
73 return git_hash_sha1_final(out, &ctx->ctx.sha1);
74 default:
75 /* unreachable */ ;
76 }
77
78 git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
79 return -1;
80 }
81
82 int git_hash_buf(
83 unsigned char *out,
84 const void *data,
85 size_t len,
86 git_hash_algorithm_t algorithm)
87 {
88 git_hash_ctx ctx;
89 int error = 0;
90
91 if (git_hash_ctx_init(&ctx, algorithm) < 0)
92 return -1;
93
94 if ((error = git_hash_update(&ctx, data, len)) >= 0)
95 error = git_hash_final(out, &ctx);
96
97 git_hash_ctx_cleanup(&ctx);
98
99 return error;
100 }
101
102 int git_hash_vec(
103 unsigned char *out,
104 git_str_vec *vec,
105 size_t n,
106 git_hash_algorithm_t algorithm)
107 {
108 git_hash_ctx ctx;
109 size_t i;
110 int error = 0;
111
112 if (git_hash_ctx_init(&ctx, algorithm) < 0)
113 return -1;
114
115 for (i = 0; i < n; i++) {
116 if ((error = git_hash_update(&ctx, vec[i].data, vec[i].len)) < 0)
117 goto done;
118 }
119
120 error = git_hash_final(out, &ctx);
121
122 done:
123 git_hash_ctx_cleanup(&ctx);
124
125 return error;
126 }
127
128 int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len)
129 {
130 static char hex[] = "0123456789abcdef";
131 char *str = out;
132 size_t i;
133
134 for (i = 0; i < hash_len; i++) {
135 *str++ = hex[hash[i] >> 4];
136 *str++ = hex[hash[i] & 0x0f];
137 }
138
139 *str++ = '\0';
140
141 return 0;
142 }