util: add SHA1 printing and comparison functions

Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13869>
This commit is contained in:
Marek Olšák 2021-11-18 17:19:39 -05:00 committed by Marge Bot
parent 0b196b40a3
commit ba8075031a
2 changed files with 44 additions and 0 deletions

View File

@ -26,6 +26,7 @@
#include "sha1/sha1.h"
#include "mesa-sha1.h"
#include <string.h>
void
_mesa_sha1_compute(const void *data, size_t size, unsigned char result[20])
@ -64,3 +65,36 @@ _mesa_sha1_hex_to_sha1(unsigned char *buf, const char *hex)
buf[i] = strtol(tmp, NULL, 16);
}
}
static void
sha1_to_uint32(const uint8_t sha1[SHA1_DIGEST_LENGTH],
uint32_t out[SHA1_DIGEST_LENGTH32])
{
memset(out, 0, SHA1_DIGEST_LENGTH);
for (unsigned i = 0; i < SHA1_DIGEST_LENGTH; i++)
out[i / 4] |= (uint32_t)sha1[i] << ((i % 4) * 8);
}
void
_mesa_sha1_print(FILE *f, const uint8_t sha1[SHA1_DIGEST_LENGTH])
{
uint32_t u32[SHA1_DIGEST_LENGTH];
sha1_to_uint32(sha1, u32);
for (unsigned i = 0; i < SHA1_DIGEST_LENGTH32; i++) {
fprintf(f, "0x%08x", u32[i]);
if (i < SHA1_DIGEST_LENGTH32 - 1)
fprintf(f, ", ");
}
}
bool
_mesa_printed_sha1_equal(const uint8_t sha1[SHA1_DIGEST_LENGTH],
const uint32_t printed_sha1[SHA1_DIGEST_LENGTH32])
{
uint32_t u32[SHA1_DIGEST_LENGTH32];
sha1_to_uint32(sha1, u32);
return memcmp(u32, printed_sha1, sizeof(u32)) == 0;
}

View File

@ -24,6 +24,8 @@
#define MESA_SHA1_H
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "c99_compat.h"
#include "sha1/sha1.h"
@ -32,6 +34,7 @@ extern "C" {
#endif
#define mesa_sha1 _SHA1_CTX
#define SHA1_DIGEST_LENGTH32 (SHA1_DIGEST_LENGTH / 4)
static inline void
_mesa_sha1_init(struct mesa_sha1 *ctx)
@ -60,6 +63,13 @@ _mesa_sha1_hex_to_sha1(unsigned char *buf, const char *hex);
void
_mesa_sha1_compute(const void *data, size_t size, unsigned char result[20]);
void
_mesa_sha1_print(FILE *f, const uint8_t sha1[SHA1_DIGEST_LENGTH]);
bool
_mesa_printed_sha1_equal(const uint8_t sha1[SHA1_DIGEST_LENGTH],
const uint32_t printed_sha1[SHA1_DIGEST_LENGTH32]);
#ifdef __cplusplus
} /* extern C */
#endif