109 lines
2.6 KiB
C
109 lines
2.6 KiB
C
#include <ctype.h>
|
|
#include <stdio.h>
|
|
|
|
#include <libft_ssl.h>
|
|
|
|
#include "compiler.h"
|
|
#include "ft_ssl.h"
|
|
#include "ft_ssl_flags.h"
|
|
#include "internal/ssl/ssl.h"
|
|
|
|
enum fmt_mode
|
|
{
|
|
FMT_STDIN,
|
|
FMT_DEFAULT,
|
|
FMT_QUIET,
|
|
FMT_REVERSE,
|
|
};
|
|
|
|
typedef void (*t_formatter)(const char *hex, const char *name_upper,
|
|
const char *label);
|
|
|
|
/* Forward declarations */
|
|
static enum fmt_mode resolve_fmt(const struct ssl_config *config,
|
|
int show_algo);
|
|
static void build_hex(char *buf, const uint8_t *digest, size_t size);
|
|
static void build_upper(char *buf, const char *src, size_t size);
|
|
static inline void fmt_stdin(const char *hex, const char *name_upper,
|
|
const char *label);
|
|
static inline void fmt_default(const char *hex, const char *name_upper,
|
|
const char *label);
|
|
static inline void fmt_quiet(const char *hex, const char *name_upper,
|
|
const char *label);
|
|
static inline void fmt_reverse(const char *hex, const char *name_upper,
|
|
const char *label);
|
|
/* ------------------- */
|
|
|
|
static const t_formatter s_formatters[] = {
|
|
[FMT_STDIN] = fmt_stdin,
|
|
[FMT_DEFAULT] = fmt_default,
|
|
[FMT_QUIET] = fmt_quiet,
|
|
[FMT_REVERSE] = fmt_reverse,
|
|
};
|
|
|
|
void
|
|
print_digest(const struct ssl_config *config, const uint8_t *digest,
|
|
const char *label, int show_algo)
|
|
{
|
|
char hex[MAX_DIGEST_SIZE * 2 + 1];
|
|
char name_upper[32];
|
|
|
|
build_hex(hex, digest, config->algo->digest_size);
|
|
build_upper(name_upper, config->algo->name, sizeof(name_upper));
|
|
s_formatters[resolve_fmt(config, show_algo)](hex, name_upper, label);
|
|
}
|
|
|
|
static enum fmt_mode
|
|
resolve_fmt(const struct ssl_config *config, int show_algo)
|
|
{
|
|
if (HAS_FLAG(config->flags, FLAG_Q))
|
|
return FMT_QUIET;
|
|
if (HAS_FLAG(config->flags, FLAG_R))
|
|
return FMT_REVERSE;
|
|
return show_algo ? FMT_DEFAULT : FMT_STDIN;
|
|
}
|
|
|
|
static void
|
|
build_hex(char *buf, const uint8_t *digest, size_t size)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < size; i++)
|
|
(void)snprintf(buf + i * 2, 3, "%02x", (unsigned int)digest[i]);
|
|
}
|
|
|
|
static void
|
|
build_upper(char *buf, const char *src, size_t size)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i + 1 < size && '\0' != src[i]; i++)
|
|
buf[i] = (char)toupper((unsigned char)src[i]);
|
|
buf[i] = '\0';
|
|
}
|
|
|
|
static inline void
|
|
fmt_stdin(const char *hex, __unused const char *name_upper, const char *label)
|
|
{
|
|
printf("(%s)= %s\n", label, hex);
|
|
}
|
|
|
|
static inline void
|
|
fmt_default(const char *hex, const char *name_upper, const char *label)
|
|
{
|
|
printf("%s (%s) = %s\n", name_upper, label, hex);
|
|
}
|
|
|
|
static inline void
|
|
fmt_quiet(const char *hex, __unused const char *name_upper,
|
|
__unused const char *label)
|
|
{
|
|
printf("%s\n", hex);
|
|
}
|
|
|
|
static inline void
|
|
fmt_reverse(const char *hex, __unused const char *name_upper,
|
|
const char *label)
|
|
{
|
|
printf("%s %s\n", hex, label);
|
|
}
|