]>
Commit | Line | Data |
---|---|---|
d6fb0924 | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
d6fb0924 ET |
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 | ||
eae0bfdc PP |
8 | #ifndef INCLUDE_hash_hash_openssl_h__ |
9 | #define INCLUDE_hash_hash_openssl_h__ | |
d6fb0924 ET |
10 | |
11 | #include "hash.h" | |
12 | ||
13 | #include <openssl/sha.h> | |
14 | ||
15 | struct git_hash_ctx { | |
16 | SHA_CTX c; | |
17 | }; | |
18 | ||
603bee07 ET |
19 | #define git_hash_ctx_init(ctx) git_hash_init(ctx) |
20 | #define git_hash_ctx_cleanup(ctx) | |
d6fb0924 | 21 | |
ac3d33df JK |
22 | GIT_INLINE(int) git_hash_global_init(void) |
23 | { | |
24 | return 0; | |
25 | } | |
26 | ||
d6fb0924 ET |
27 | GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx) |
28 | { | |
29 | assert(ctx); | |
eae0bfdc PP |
30 | |
31 | if (SHA1_Init(&ctx->c) != 1) { | |
ac3d33df | 32 | git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to initialize hash context"); |
eae0bfdc PP |
33 | return -1; |
34 | } | |
35 | ||
d6fb0924 ET |
36 | return 0; |
37 | } | |
38 | ||
39 | GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) | |
40 | { | |
41 | assert(ctx); | |
eae0bfdc PP |
42 | |
43 | if (SHA1_Update(&ctx->c, data, len) != 1) { | |
ac3d33df | 44 | git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to update hash"); |
eae0bfdc PP |
45 | return -1; |
46 | } | |
47 | ||
d6fb0924 ET |
48 | return 0; |
49 | } | |
50 | ||
51 | GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx) | |
52 | { | |
53 | assert(ctx); | |
eae0bfdc PP |
54 | |
55 | if (SHA1_Final(out->id, &ctx->c) != 1) { | |
ac3d33df | 56 | git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to finalize hash"); |
eae0bfdc PP |
57 | return -1; |
58 | } | |
59 | ||
d6fb0924 ET |
60 | return 0; |
61 | } | |
62 | ||
eae0bfdc | 63 | #endif |