]> git.proxmox.com Git - libgit2.git/blame - src/hash/hash_common_crypto.h
New upstream version 0.28.1+dfsg.1
[libgit2.git] / src / hash / hash_common_crypto.h
CommitLineData
d9c0dbb0
CMN
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
eae0bfdc
PP
8#ifndef INCLUDE_hash_hash_common_crypto_h__
9#define INCLUDE_hash_hash_common_crypto_h__
d9c0dbb0
CMN
10
11#include "hash.h"
12
13#include <CommonCrypto/CommonDigest.h>
14
15struct git_hash_ctx {
16 CC_SHA1_CTX c;
17};
18
eae0bfdc
PP
19#define CC_LONG_MAX ((CC_LONG)-1)
20
d9c0dbb0
CMN
21#define git_hash_ctx_init(ctx) git_hash_init(ctx)
22#define git_hash_ctx_cleanup(ctx)
23
ac3d33df
JK
24GIT_INLINE(int) git_hash_global_init(void)
25{
26 return 0;
27}
28
d9c0dbb0
CMN
29GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
30{
31 assert(ctx);
32 CC_SHA1_Init(&ctx->c);
33 return 0;
34}
35
eae0bfdc 36GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len)
d9c0dbb0 37{
eae0bfdc
PP
38 const unsigned char *data = _data;
39
d9c0dbb0 40 assert(ctx);
eae0bfdc
PP
41
42 while (len > 0) {
43 CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len;
44
45 CC_SHA1_Update(&ctx->c, data, chunk);
46
47 data += chunk;
48 len -= chunk;
49 }
50
d9c0dbb0
CMN
51 return 0;
52}
53
54GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
55{
56 assert(ctx);
57 CC_SHA1_Final(out->id, &ctx->c);
58 return 0;
59}
60
eae0bfdc 61#endif