Introduce virCryptoHashBuf

A function that keeps the hash in binary form instead of converting
it to human-readable hexadecimal form.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Ján Tomko 2018-05-11 16:31:10 +02:00
parent 95ba1c2f6e
commit 0eeedd61a9
3 changed files with 29 additions and 10 deletions

View File

@ -1648,6 +1648,7 @@ virConfWriteMem;
# util/vircrypto.h # util/vircrypto.h
virCryptoEncryptData; virCryptoEncryptData;
virCryptoGenerateRandom; virCryptoGenerateRandom;
virCryptoHashBuf;
virCryptoHashString; virCryptoHashString;
virCryptoHaveCipher; virCryptoHaveCipher;

View File

@ -53,6 +53,26 @@ struct virHashInfo {
verify(ARRAY_CARDINALITY(hashinfo) == VIR_CRYPTO_HASH_LAST); verify(ARRAY_CARDINALITY(hashinfo) == VIR_CRYPTO_HASH_LAST);
int
virCryptoHashBuf(virCryptoHash hash,
const char *input,
unsigned char *output)
{
if (hash >= VIR_CRYPTO_HASH_LAST) {
virReportError(VIR_ERR_INVALID_ARG,
_("Unknown crypto hash %d"), hash);
return -1;
}
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 int
virCryptoHashString(virCryptoHash hash, virCryptoHashString(virCryptoHash hash,
const char *input, const char *input,
@ -62,20 +82,11 @@ virCryptoHashString(virCryptoHash hash,
size_t hashstrlen; size_t hashstrlen;
size_t i; size_t i;
if (hash >= VIR_CRYPTO_HASH_LAST) { if (virCryptoHashBuf(hash, input, buf) < 0)
virReportError(VIR_ERR_INVALID_ARG,
_("Unknown crypto hash %d"), hash);
return -1; return -1;
}
hashstrlen = (hashinfo[hash].hashlen * 2) + 1; hashstrlen = (hashinfo[hash].hashlen * 2) + 1;
if (!(hashinfo[hash].func(input, strlen(input), buf))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to compute hash of data"));
return -1;
}
if (VIR_ALLOC_N(*output, hashstrlen) < 0) if (VIR_ALLOC_N(*output, hashstrlen) < 0)
return -1; return -1;

View File

@ -41,6 +41,13 @@ typedef enum {
VIR_CRYPTO_CIPHER_LAST VIR_CRYPTO_CIPHER_LAST
} virCryptoCipher; } virCryptoCipher;
int
virCryptoHashBuf(virCryptoHash hash,
const char *input,
unsigned char *output)
ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
ATTRIBUTE_RETURN_CHECK;
int int
virCryptoHashString(virCryptoHash hash, virCryptoHashString(virCryptoHash hash,
const char *input, const char *input,