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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
diff --git a/Makefile b/Makefile
index 4063e6c..850cf44 100644
--- a/Makefile
+++ b/Makefile
@@ -98,7 +98,7 @@ $(TEST_EXE): tests/%.exe: tests/%.o tests/%_main.o
$(CC) -o $@ \
$(patsubst %.exe,%_main.o,$@) \
$(patsubst %.exe,%.o,$@) \
- $(T_LIB) -L. -lgit2 -lz
+ $(T_LIB) -L. -lgit2 -lz -lcrypto
$(TEST_RUN): tests/%.run: tests/%.exe
@t=trash-$(<F) && \
diff --git a/src/hash.c b/src/hash.c
new file mode 100644
index 0000000..4ff1dd0
--- /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);
+}
diff --git a/src/hash.h b/src/hash.h
new file mode 100644
index 0000000..b371c05
--- /dev/null
+++ b/src/hash.h
@@ -0,0 +1,26 @@
+/*
+ * hash.h
+ */
+#ifndef INCLUDE_hash_h__
+#define INCLUDE_hash_h__
+
+#include "git/oid.h"
+
+typedef struct git_hash_ctx git_hash_ctx;
+
+typedef struct {
+ void *data;
+ size_t len;
+} git_buf_vec;
+
+git_hash_ctx *git_hash_new_ctx(void);
+void git_hash_free_ctx(git_hash_ctx *ctx);
+
+void git_hash_init(git_hash_ctx *c);
+void git_hash_update(git_hash_ctx *c, const void *data, size_t len);
+void git_hash_final(git_oid *out, git_hash_ctx *c);
+
+void git_hash_buf(git_oid *out, const void *data, size_t len);
+void git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n);
+
+#endif /* INCLUDE_hash_h__ */
diff --git a/tests/t0000-hash.c b/tests/t0000-hash.c
new file mode 100644
index 0000000..6e9df71
--- /dev/null
+++ b/tests/t0000-hash.c
@@ -0,0 +1,57 @@
+#include "test_lib.h"
+#include "hash.h"
+#include <git/oid.h>
+
+static char *hello_id = "22596363b3de40b06f981fb85d82312e8c0ed511";
+static char *hello_text = "hello world\n";
+
+static char *bye_id = "ce08fe4884650f067bd5703b6a59a8b3b3c99a09";
+static char *bye_text = "bye world\n";
+
+BEGIN_TEST(hash_iuf)
+ git_hash_ctx *ctx;
+ git_oid id1, id2;
+
+ must_be_true((ctx = git_hash_new_ctx()) != NULL);
+
+ /* should already be init'd */
+ git_hash_update(ctx, hello_text, strlen(hello_text));
+ git_hash_final(&id2, ctx);
+ must_pass(git_oid_mkstr(&id1, hello_id));
+ must_be_true(git_oid_cmp(&id1, &id2) == 0);
+
+ /* reinit should permit reuse */
+ git_hash_init(ctx);
+ git_hash_update(ctx, bye_text, strlen(bye_text));
+ git_hash_final(&id2, ctx);
+ must_pass(git_oid_mkstr(&id1, bye_id));
+ must_be_true(git_oid_cmp(&id1, &id2) == 0);
+
+ git_hash_free_ctx(ctx);
+END_TEST
+
+BEGIN_TEST(hash_buf)
+ git_oid id1, id2;
+
+ must_pass(git_oid_mkstr(&id1, hello_id));
+
+ git_hash_buf(&id2, hello_text, strlen(hello_text));
+
+ must_be_true(git_oid_cmp(&id1, &id2) == 0);
+END_TEST
+
+BEGIN_TEST(hash_vec)
+ git_oid id1, id2;
+ git_buf_vec vec[2];
+
+ must_pass(git_oid_mkstr(&id1, hello_id));
+
+ vec[0].data = hello_text;
+ vec[0].len = 4;
+ vec[1].data = hello_text+4;
+ vec[1].len = strlen(hello_text)-4;
+
+ git_hash_vec(&id2, vec, 2);
+
+ must_be_true(git_oid_cmp(&id1, &id2) == 0);
+END_TEST