Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame^] | 1 | From 92e8378103bba3b91f2dec4e6fda3e1755a7c0fd Mon Sep 17 00:00:00 2001 |
| 2 | From: Paul Barker <paul@paulbarker.me.uk> |
| 3 | Date: Sat, 7 Nov 2015 10:23:51 +0000 |
| 4 | Subject: [PATCH 3/4] sha256: Add sha256_to_string function |
| 5 | |
| 6 | Signed-off-by: Paul Barker <paul@paulbarker.me.uk> |
| 7 | Signed-off-by: Alejandro del Castillo <alejandro.delcastillo@ni.com> |
| 8 | |
| 9 | Upstream-Status: Accepted |
| 10 | --- |
| 11 | libopkg/file_util.c | 28 +++------------------------- |
| 12 | libopkg/sha256.c | 7 +++++++ |
| 13 | libopkg/sha256.h | 3 +++ |
| 14 | 3 files changed, 13 insertions(+), 25 deletions(-) |
| 15 | |
| 16 | diff --git a/libopkg/file_util.c b/libopkg/file_util.c |
| 17 | index cb3dbf0..864aedb 100644 |
| 18 | --- a/libopkg/file_util.c |
| 19 | +++ b/libopkg/file_util.c |
| 20 | @@ -374,27 +374,13 @@ char *file_md5sum_alloc(const char *file_name) |
| 21 | #ifdef HAVE_SHA256 |
| 22 | char *file_sha256sum_alloc(const char *file_name) |
| 23 | { |
| 24 | - static const int sha256sum_bin_len = 32; |
| 25 | - static const int sha256sum_hex_len = 64; |
| 26 | - |
| 27 | - static const unsigned char bin2hex[16] = { |
| 28 | - '0', '1', '2', '3', |
| 29 | - '4', '5', '6', '7', |
| 30 | - '8', '9', 'a', 'b', |
| 31 | - 'c', 'd', 'e', 'f' |
| 32 | - }; |
| 33 | - |
| 34 | - int i, err; |
| 35 | + int err; |
| 36 | FILE *file; |
| 37 | - char *sha256sum_hex; |
| 38 | - unsigned char sha256sum_bin[sha256sum_bin_len]; |
| 39 | - |
| 40 | - sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1); |
| 41 | + unsigned char sha256sum_bin[32]; |
| 42 | |
| 43 | file = fopen(file_name, "r"); |
| 44 | if (file == NULL) { |
| 45 | opkg_perror(ERROR, "Failed to open file %s", file_name); |
| 46 | - free(sha256sum_hex); |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | @@ -402,20 +388,12 @@ char *file_sha256sum_alloc(const char *file_name) |
| 51 | if (err) { |
| 52 | opkg_msg(ERROR, "Could't compute sha256sum for %s.\n", file_name); |
| 53 | fclose(file); |
| 54 | - free(sha256sum_hex); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | fclose(file); |
| 59 | |
| 60 | - for (i = 0; i < sha256sum_bin_len; i++) { |
| 61 | - sha256sum_hex[i * 2] = bin2hex[sha256sum_bin[i] >> 4]; |
| 62 | - sha256sum_hex[i * 2 + 1] = bin2hex[sha256sum_bin[i] & 0xf]; |
| 63 | - } |
| 64 | - |
| 65 | - sha256sum_hex[sha256sum_hex_len] = '\0'; |
| 66 | - |
| 67 | - return sha256sum_hex; |
| 68 | + return sha256_to_string(sha256sum_bin); |
| 69 | } |
| 70 | |
| 71 | #endif |
| 72 | diff --git a/libopkg/sha256.c b/libopkg/sha256.c |
| 73 | index 0816858..bceed72 100644 |
| 74 | --- a/libopkg/sha256.c |
| 75 | +++ b/libopkg/sha256.c |
| 76 | @@ -29,6 +29,8 @@ |
| 77 | #include <stddef.h> |
| 78 | #include <string.h> |
| 79 | |
| 80 | +#include "string_util.h" |
| 81 | + |
| 82 | #if USE_UNLOCKED_IO |
| 83 | #include "unlocked-io.h" |
| 84 | #endif |
| 85 | @@ -517,3 +519,8 @@ void sha256_process_block(const void *buffer, size_t len, |
| 86 | h = ctx->state[7] += h; |
| 87 | } |
| 88 | } |
| 89 | + |
| 90 | +char *sha256_to_string(const void *sha256sum_bin) |
| 91 | +{ |
| 92 | + return bin_to_hex(sha256sum_bin, 32); |
| 93 | +} |
| 94 | diff --git a/libopkg/sha256.h b/libopkg/sha256.h |
| 95 | index 734ab54..0d1e9e5 100644 |
| 96 | --- a/libopkg/sha256.h |
| 97 | +++ b/libopkg/sha256.h |
| 98 | @@ -85,6 +85,9 @@ extern int sha224_stream(FILE * stream, void *resblock); |
| 99 | extern void *sha256_buffer(const char *buffer, size_t len, void *resblock); |
| 100 | extern void *sha224_buffer(const char *buffer, size_t len, void *resblock); |
| 101 | |
| 102 | +/* Convert a binary sha256sum value to an ASCII string. */ |
| 103 | +char *sha256_to_string(const void *sha256sum_bin); |
| 104 | + |
| 105 | #ifdef __cplusplus |
| 106 | } |
| 107 | #endif |
| 108 | -- |
| 109 | 1.9.1 |
| 110 | |