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:
parent
0b196b40a3
commit
ba8075031a
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#include "sha1/sha1.h"
|
#include "sha1/sha1.h"
|
||||||
#include "mesa-sha1.h"
|
#include "mesa-sha1.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
void
|
void
|
||||||
_mesa_sha1_compute(const void *data, size_t size, unsigned char result[20])
|
_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);
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -24,6 +24,8 @@
|
||||||
#define MESA_SHA1_H
|
#define MESA_SHA1_H
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include "c99_compat.h"
|
#include "c99_compat.h"
|
||||||
#include "sha1/sha1.h"
|
#include "sha1/sha1.h"
|
||||||
|
|
||||||
|
@ -32,6 +34,7 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define mesa_sha1 _SHA1_CTX
|
#define mesa_sha1 _SHA1_CTX
|
||||||
|
#define SHA1_DIGEST_LENGTH32 (SHA1_DIGEST_LENGTH / 4)
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
_mesa_sha1_init(struct mesa_sha1 *ctx)
|
_mesa_sha1_init(struct mesa_sha1 *ctx)
|
||||||
|
@ -60,6 +63,13 @@ _mesa_sha1_hex_to_sha1(unsigned char *buf, const char *hex);
|
||||||
void
|
void
|
||||||
_mesa_sha1_compute(const void *data, size_t size, unsigned char result[20]);
|
_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
|
#ifdef __cplusplus
|
||||||
} /* extern C */
|
} /* extern C */
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue