From 0eeedd61a9ae20375b1503454b9749f108740bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Tomko?= Date: Fri, 11 May 2018 16:31:10 +0200 Subject: [PATCH] Introduce virCryptoHashBuf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A function that keeps the hash in binary form instead of converting it to human-readable hexadecimal form. Signed-off-by: Ján Tomko --- src/libvirt_private.syms | 1 + src/util/vircrypto.c | 31 +++++++++++++++++++++---------- src/util/vircrypto.h | 7 +++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d28a751ebd..5b2a58cdcd 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1648,6 +1648,7 @@ virConfWriteMem; # util/vircrypto.h virCryptoEncryptData; virCryptoGenerateRandom; +virCryptoHashBuf; virCryptoHashString; virCryptoHaveCipher; diff --git a/src/util/vircrypto.c b/src/util/vircrypto.c index 48b04fc8ce..1a2dcc28b7 100644 --- a/src/util/vircrypto.c +++ b/src/util/vircrypto.c @@ -54,28 +54,39 @@ struct virHashInfo { verify(ARRAY_CARDINALITY(hashinfo) == VIR_CRYPTO_HASH_LAST); int -virCryptoHashString(virCryptoHash hash, - const char *input, - char **output) +virCryptoHashBuf(virCryptoHash hash, + const char *input, + unsigned char *output) { - unsigned char buf[VIR_CRYPTO_LARGEST_DIGEST_SIZE]; - size_t hashstrlen; - size_t i; - if (hash >= VIR_CRYPTO_HASH_LAST) { virReportError(VIR_ERR_INVALID_ARG, _("Unknown crypto hash %d"), hash); return -1; } - hashstrlen = (hashinfo[hash].hashlen * 2) + 1; - - if (!(hashinfo[hash].func(input, strlen(input), buf))) { + if (!(hashinfo[hash].func(input, strlen(input), output))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to compute hash of data")); return -1; } + return 0; +} + +int +virCryptoHashString(virCryptoHash hash, + const char *input, + char **output) +{ + unsigned char buf[VIR_CRYPTO_LARGEST_DIGEST_SIZE]; + size_t hashstrlen; + size_t i; + + if (virCryptoHashBuf(hash, input, buf) < 0) + return -1; + + hashstrlen = (hashinfo[hash].hashlen * 2) + 1; + if (VIR_ALLOC_N(*output, hashstrlen) < 0) return -1; diff --git a/src/util/vircrypto.h b/src/util/vircrypto.h index 81743d2f74..64984006be 100644 --- a/src/util/vircrypto.h +++ b/src/util/vircrypto.h @@ -41,6 +41,13 @@ typedef enum { VIR_CRYPTO_CIPHER_LAST } virCryptoCipher; +int +virCryptoHashBuf(virCryptoHash hash, + const char *input, + unsigned char *output) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) + ATTRIBUTE_RETURN_CHECK; + int virCryptoHashString(virCryptoHash hash, const char *input, -- GitLab