2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 int git_hash_global_init(void)
12 return git_hash_sha1_global_init();
15 int git_hash_ctx_init(git_hash_ctx
*ctx
, git_hash_algorithm_t algorithm
)
20 case GIT_HASH_ALGORITHM_SHA1
:
21 error
= git_hash_sha1_ctx_init(&ctx
->ctx
.sha1
);
24 git_error_set(GIT_ERROR_INTERNAL
, "unknown hash algorithm");
28 ctx
->algorithm
= algorithm
;
32 void git_hash_ctx_cleanup(git_hash_ctx
*ctx
)
34 switch (ctx
->algorithm
) {
35 case GIT_HASH_ALGORITHM_SHA1
:
36 git_hash_sha1_ctx_cleanup(&ctx
->ctx
.sha1
);
43 int git_hash_init(git_hash_ctx
*ctx
)
45 switch (ctx
->algorithm
) {
46 case GIT_HASH_ALGORITHM_SHA1
:
47 return git_hash_sha1_init(&ctx
->ctx
.sha1
);
52 git_error_set(GIT_ERROR_INTERNAL
, "unknown hash algorithm");
56 int git_hash_update(git_hash_ctx
*ctx
, const void *data
, size_t len
)
58 switch (ctx
->algorithm
) {
59 case GIT_HASH_ALGORITHM_SHA1
:
60 return git_hash_sha1_update(&ctx
->ctx
.sha1
, data
, len
);
65 git_error_set(GIT_ERROR_INTERNAL
, "unknown hash algorithm");
69 int git_hash_final(unsigned char *out
, git_hash_ctx
*ctx
)
71 switch (ctx
->algorithm
) {
72 case GIT_HASH_ALGORITHM_SHA1
:
73 return git_hash_sha1_final(out
, &ctx
->ctx
.sha1
);
78 git_error_set(GIT_ERROR_INTERNAL
, "unknown hash algorithm");
86 git_hash_algorithm_t algorithm
)
91 if (git_hash_ctx_init(&ctx
, algorithm
) < 0)
94 if ((error
= git_hash_update(&ctx
, data
, len
)) >= 0)
95 error
= git_hash_final(out
, &ctx
);
97 git_hash_ctx_cleanup(&ctx
);
106 git_hash_algorithm_t algorithm
)
112 if (git_hash_ctx_init(&ctx
, algorithm
) < 0)
115 for (i
= 0; i
< n
; i
++) {
116 if ((error
= git_hash_update(&ctx
, vec
[i
].data
, vec
[i
].len
)) < 0)
120 error
= git_hash_final(out
, &ctx
);
123 git_hash_ctx_cleanup(&ctx
);
128 int git_hash_fmt(char *out
, unsigned char *hash
, size_t hash_len
)
130 static char hex
[] = "0123456789abcdef";
134 for (i
= 0; i
< hash_len
; i
++) {
135 *str
++ = hex
[hash
[i
] >> 4];
136 *str
++ = hex
[hash
[i
] & 0x0f];