diff options
author | Ramsay Jones <ramsay@ramsay1.demon.co.uk> | 2008-12-27 18:58:25 +0000 |
---|---|---|
committer | Shawn O. Pearce <spearce@spearce.org> | 2008-12-30 07:48:10 -0800 |
commit | 007e075337848055a92e218bdfe137451a4c9635 (patch) | |
tree | e896d00181e2053cf3cf6441732a13f6ca83f104 /src/hash.c | |
parent | 42fd40db68e5e5797531dc1f5de7aef39a2262c0 (diff) | |
download | libgit2-007e075337848055a92e218bdfe137451a4c9635.tar.gz |
Add some routines for SHA1 hash computation
[sp: Changed signature for output to use git_oid, and added
a test case to verify an allocated git_hash_ctx can be
reinitialized and reused.]
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'src/hash.c')
-rw-r--r-- | src/hash.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/hash.c b/src/hash.c new file mode 100644 index 000000000..4ff1dd099 --- /dev/null +++ b/src/hash.c @@ -0,0 +1,87 @@ +/* + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, + * as published by the Free Software Foundation. + * + * In addition to the permissions in the GNU General Public License, + * the authors give you unlimited permission to link the compiled + * version of this file into combinations with other programs, + * and to distribute those combinations without any restriction + * coming from the use of this file. (The General Public License + * restrictions do apply in other respects; for example, they cover + * modification of the file, and distribution when not linked into + * a combined executable.) + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "common.h" +#include "hash.h" +#include <openssl/sha.h> + +struct git_hash_ctx { + SHA_CTX c; +}; + +git_hash_ctx *git_hash_new_ctx(void) +{ + git_hash_ctx *ctx = malloc(sizeof(*ctx)); + + if (!ctx) + return NULL; + + SHA1_Init(&ctx->c); + + return ctx; +} + +void git_hash_free_ctx(git_hash_ctx *ctx) +{ + free(ctx); +} + +void git_hash_init(git_hash_ctx *ctx) +{ + assert(ctx); + SHA1_Init(&ctx->c); +} + +void git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) +{ + assert(ctx); + SHA1_Update(&ctx->c, data, len); +} + +void git_hash_final(git_oid *out, git_hash_ctx *ctx) +{ + assert(ctx); + SHA1_Final(out->id, &ctx->c); +} + +void git_hash_buf(git_oid *out, const void *data, size_t len) +{ + SHA_CTX c; + + SHA1_Init(&c); + SHA1_Update(&c, data, len); + SHA1_Final(out->id, &c); +} + +void git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n) +{ + SHA_CTX c; + size_t i; + + SHA1_Init(&c); + for (i = 0; i < n; i++) + SHA1_Update(&c, vec[i].data, vec[i].len); + SHA1_Final(out->id, &c); +} |