| 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 |
87 |
| int git_hash_global_init(void) |
| 11 |
|
| { |
| 12 |
174 |
| if (git_hash_sha1_global_init() < 0 || |
| 13 |
87 |
| git_hash_sha256_global_init() < 0) |
| 14 |
0 |
| return -1; |
| 15 |
|
| |
| 16 |
87 |
| return 0; |
| 17 |
|
| } |
| 18 |
|
| |
| 19 |
1540 |
| int git_hash_ctx_init(git_hash_ctx *ctx, git_hash_algorithm_t algorithm) |
| 20 |
|
| { |
| 21 |
|
| int error; |
| 22 |
|
| |
| 23 |
1540 |
| switch (algorithm) { |
| 24 |
1540 |
| case GIT_HASH_ALGORITHM_SHA1: |
| 25 |
1540 |
| error = git_hash_sha1_ctx_init(&ctx->ctx.sha1); |
| 26 |
1540 |
| break; |
| 27 |
0 |
| case GIT_HASH_ALGORITHM_SHA256: |
| 28 |
0 |
| error = git_hash_sha256_ctx_init(&ctx->ctx.sha256); |
| 29 |
0 |
| break; |
| 30 |
0 |
| default: |
| 31 |
0 |
| git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); |
| 32 |
0 |
| error = -1; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
1540 |
| ctx->algorithm = algorithm; |
| 36 |
1540 |
| return error; |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
1540 |
| void git_hash_ctx_cleanup(git_hash_ctx *ctx) |
| 40 |
|
| { |
| 41 |
1540 |
| switch (ctx->algorithm) { |
| 42 |
1540 |
| case GIT_HASH_ALGORITHM_SHA1: |
| 43 |
1540 |
| git_hash_sha1_ctx_cleanup(&ctx->ctx.sha1); |
| 44 |
1540 |
| return; |
| 45 |
0 |
| case GIT_HASH_ALGORITHM_SHA256: |
| 46 |
0 |
| git_hash_sha256_ctx_cleanup(&ctx->ctx.sha256); |
| 47 |
0 |
| return; |
| 48 |
|
| default: |
| 49 |
|
| /* unreachable */ ; |
| 50 |
|
| } |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
47 |
| int git_hash_init(git_hash_ctx *ctx) |
| 54 |
|
| { |
| 55 |
47 |
| switch (ctx->algorithm) { |
| 56 |
47 |
| case GIT_HASH_ALGORITHM_SHA1: |
| 57 |
47 |
| return git_hash_sha1_init(&ctx->ctx.sha1); |
| 58 |
0 |
| case GIT_HASH_ALGORITHM_SHA256: |
| 59 |
0 |
| return git_hash_sha256_init(&ctx->ctx.sha256); |
| 60 |
0 |
| default: |
| 61 |
|
| /* unreachable */ ; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
0 |
| git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); |
| 65 |
0 |
| return -1; |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
3064 |
| int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) |
| 69 |
|
| { |
| 70 |
3064 |
| switch (ctx->algorithm) { |
| 71 |
3064 |
| case GIT_HASH_ALGORITHM_SHA1: |
| 72 |
3064 |
| return git_hash_sha1_update(&ctx->ctx.sha1, data, len); |
| 73 |
0 |
| case GIT_HASH_ALGORITHM_SHA256: |
| 74 |
0 |
| return git_hash_sha256_update(&ctx->ctx.sha256, data, len); |
| 75 |
0 |
| default: |
| 76 |
|
| /* unreachable */ ; |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
0 |
| git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); |
| 80 |
0 |
| return -1; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
1528 |
| int git_hash_final(unsigned char *out, git_hash_ctx *ctx) |
| 84 |
|
| { |
| 85 |
1528 |
| switch (ctx->algorithm) { |
| 86 |
1528 |
| case GIT_HASH_ALGORITHM_SHA1: |
| 87 |
1528 |
| return git_hash_sha1_final(out, &ctx->ctx.sha1); |
| 88 |
0 |
| case GIT_HASH_ALGORITHM_SHA256: |
| 89 |
0 |
| return git_hash_sha256_final(out, &ctx->ctx.sha256); |
| 90 |
0 |
| default: |
| 91 |
|
| /* unreachable */ ; |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
0 |
| git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); |
| 95 |
0 |
| return -1; |
| 96 |
|
| } |
| 97 |
|
| |
| 98 |
186 |
| int git_hash_buf( |
| 99 |
|
| unsigned char *out, |
| 100 |
|
| const void *data, |
| 101 |
|
| size_t len, |
| 102 |
|
| git_hash_algorithm_t algorithm) |
| 103 |
|
| { |
| 104 |
|
| git_hash_ctx ctx; |
| 105 |
186 |
| int error = 0; |
| 106 |
|
| |
| 107 |
186 |
▶ | if (git_hash_ctx_init(&ctx, algorithm) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_hash_ctx_init(&ctx, algorithm) < 0) |
0 |
186 |
186 |
|
| 108 |
0 |
| return -1; |
| 109 |
|
| |
| 110 |
186 |
▶ | if ((error = git_hash_update(&ctx, data, len)) >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_hash_update(&ctx, data, len)) >= 0) |
186 |
0 |
186 |
|
| 111 |
186 |
| error = git_hash_final(out, &ctx); |
| 112 |
|
| |
| 113 |
186 |
| git_hash_ctx_cleanup(&ctx); |
| 114 |
|
| |
| 115 |
186 |
| return error; |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
994 |
| int git_hash_vec( |
| 119 |
|
| unsigned char *out, |
| 120 |
|
| git_str_vec *vec, |
| 121 |
|
| size_t n, |
| 122 |
|
| git_hash_algorithm_t algorithm) |
| 123 |
|
| { |
| 124 |
|
| git_hash_ctx ctx; |
| 125 |
|
| size_t i; |
| 126 |
994 |
| int error = 0; |
| 127 |
|
| |
| 128 |
994 |
▶ | if (git_hash_ctx_init(&ctx, algorithm) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_hash_ctx_init(&ctx, algorithm) < 0) |
0 |
994 |
994 |
|
| 129 |
0 |
| return -1; |
| 130 |
|
| |
| 131 |
2982 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
1988 |
994 |
2982 |
|
| 132 |
1988 |
▶ | if ((error = git_hash_update(&ctx, vec[i].data, vec[i].len)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_hash_update(&ctx, vec[i].data, vec[i].len)) < 0) |
0 |
1988 |
1988 |
|
| 133 |
0 |
| goto done; |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
994 |
| error = git_hash_final(out, &ctx); |
| 137 |
|
| |
| 138 |
994 |
| done: |
| 139 |
994 |
| git_hash_ctx_cleanup(&ctx); |
| 140 |
|
| |
| 141 |
994 |
| return error; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
5 |
| int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len) |
| 145 |
|
| { |
| 146 |
|
| static char hex[] = "0123456789abcdef"; |
| 147 |
5 |
| char *str = out; |
| 148 |
|
| size_t i; |
| 149 |
|
| |
| 150 |
105 |
▶ | for (i = 0; i < hash_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < hash_len; i++) { |
100 |
5 |
105 |
|
| 151 |
100 |
| *str++ = hex[hash[i] >> 4]; |
| 152 |
100 |
| *str++ = hex[hash[i] & 0x0f]; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
5 |
| *str++ = '\0'; |
| 156 |
|
| |
| 157 |
5 |
| return 0; |
| 158 |
|
| } |