[SATYR PATCH 1/3] Higher-level function for SHA1 hashes in hex encoding

Martin Milata mmilata at redhat.com
Wed Jun 26 14:13:51 UTC 2013


Signed-off-by: Martin Milata <mmilata at redhat.com>
---
 lib/core_fingerprint.c | 10 +---------
 lib/sha1.c             | 16 ++++++++++++++++
 lib/sha1.h             |  5 +++++
 3 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/lib/core_fingerprint.c b/lib/core_fingerprint.c
index 5e52c30..aeea837 100644
--- a/lib/core_fingerprint.c
+++ b/lib/core_fingerprint.c
@@ -562,15 +562,7 @@ hash_frame (struct sr_core_frame *frame)
     if (!frame->fingerprint)
         return;
 
-    struct sr_sha1_state ctx;
-    char bin_hash[SR_SHA1_RESULT_BIN_LEN];
-    char *hash = sr_malloc(SR_SHA1_RESULT_LEN);
-
-    sr_sha1_begin(&ctx);
-    sr_sha1_hash(&ctx, frame->fingerprint, strlen(frame->fingerprint));
-    sr_sha1_end(&ctx, bin_hash);
-    sr_bin2hex(hash, bin_hash, sizeof(bin_hash))[0] = '\0';
-
+    char *hash = sr_sha1_hash_string(frame->fingerprint);
     free(frame->fingerprint);
     frame->fingerprint = hash;
     frame->fingerprint_hashed = true;
diff --git a/lib/sha1.c b/lib/sha1.c
index 3c781ed..0f21fe8 100644
--- a/lib/sha1.c
+++ b/lib/sha1.c
@@ -27,6 +27,7 @@
     stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
 */
 #include "sha1.h"
+#include "utils.h"
 
 #if defined(__BIG_ENDIAN__) && __BIG_ENDIAN__
 # define SHA1_BIG_ENDIAN 1
@@ -236,3 +237,18 @@ common64_end(struct sr_sha1_state *state,
         bufpos = 0;
     }
 }
+
+char *
+sr_sha1_hash_string(const char *str)
+{
+    struct sr_sha1_state ctx;
+    char bin_hash[SR_SHA1_RESULT_BIN_LEN];
+    char *hex_hash = sr_malloc(SR_SHA1_RESULT_LEN);
+
+    sr_sha1_begin(&ctx);
+    sr_sha1_hash(&ctx, str, strlen(str));
+    sr_sha1_end(&ctx, bin_hash);
+    sr_bin2hex(hex_hash, bin_hash, sizeof(bin_hash))[0] = '\0';
+
+    return hex_hash;
+}
diff --git a/lib/sha1.h b/lib/sha1.h
index 6220c15..0a647c8 100644
--- a/lib/sha1.h
+++ b/lib/sha1.h
@@ -76,6 +76,11 @@ sr_sha1_hash(struct sr_sha1_state *state,
 void
 sr_sha1_end(struct sr_sha1_state *state, void *resbuf);
 
+/* High level function that hashes C string and returns hexadecimal encoding of
+ * the hash. */
+char *
+sr_sha1_hash_string(const char *str);
+
 #ifdef __cplusplus
 }
 #endif
-- 
1.7.11.7



More information about the Crash-catcher mailing list