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 "common_crypto.h"
9 
10 #define CC_LONG_MAX ((CC_LONG)-1)
11 
git_hash_sha1_global_init(void)12 int git_hash_sha1_global_init(void)
13 {
14 	return 0;
15 }
16 
git_hash_sha1_ctx_init(git_hash_sha1_ctx * ctx)17 int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
18 {
19 	return git_hash_sha1_init(ctx);
20 }
21 
git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx * ctx)22 void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
23 {
24 	GIT_UNUSED(ctx);
25 }
26 
git_hash_sha1_init(git_hash_sha1_ctx * ctx)27 int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
28 {
29 	GIT_ASSERT_ARG(ctx);
30 	CC_SHA1_Init(&ctx->c);
31 	return 0;
32 }
33 
git_hash_sha1_update(git_hash_sha1_ctx * ctx,const void * _data,size_t len)34 int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len)
35 {
36 	const unsigned char *data = _data;
37 
38 	GIT_ASSERT_ARG(ctx);
39 
40 	while (len > 0) {
41 		CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len;
42 
43 		CC_SHA1_Update(&ctx->c, data, chunk);
44 
45 		data += chunk;
46 		len -= chunk;
47 	}
48 
49 	return 0;
50 }
51 
git_hash_sha1_final(git_oid * out,git_hash_sha1_ctx * ctx)52 int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
53 {
54 	GIT_ASSERT_ARG(ctx);
55 	CC_SHA1_Final(out->id, &ctx->c);
56 	return 0;
57 }
58