Added t0501-walk (simple test for all revision pool walking modes)
[libgit2.git] / src / hash.c
blob1ddf7a3bc63dd2ffbd9438c3b25881de9ac0bff3
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
26 #include "common.h"
27 #include "hash.h"
28 #include "sha1.h"
30 struct git_hash_ctx {
31 SHA_CTX c;
34 git_hash_ctx *git_hash_new_ctx(void)
36 git_hash_ctx *ctx = git__malloc(sizeof(*ctx));
38 if (!ctx)
39 return NULL;
41 SHA1_Init(&ctx->c);
43 return ctx;
46 void git_hash_free_ctx(git_hash_ctx *ctx)
48 free(ctx);
51 void git_hash_init(git_hash_ctx *ctx)
53 assert(ctx);
54 SHA1_Init(&ctx->c);
57 void git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
59 assert(ctx);
60 SHA1_Update(&ctx->c, data, len);
63 void git_hash_final(git_oid *out, git_hash_ctx *ctx)
65 assert(ctx);
66 SHA1_Final(out->id, &ctx->c);
69 void git_hash_buf(git_oid *out, const void *data, size_t len)
71 SHA_CTX c;
73 SHA1_Init(&c);
74 SHA1_Update(&c, data, len);
75 SHA1_Final(out->id, &c);
78 void git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n)
80 SHA_CTX c;
81 size_t i;
83 SHA1_Init(&c);
84 for (i = 0; i < n; i++)
85 SHA1_Update(&c, vec[i].data, vec[i].len);
86 SHA1_Final(out->id, &c);