#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "random.h"
#include "util.h"

#define ALGORITHM "argon2id"
#define ITERATIONS 5
#define MEMORY 12

static void
usage(char *argv_0)
{
	fprintf(stderr, "Usage: %s [-d difficulty]\n", argv_0);
	exit(1);
}

int
main(int argc, char *argv[])
{
	int digits = 4;

	int c;
	char *endptr;
	while ((c = getopt(argc, argv, "d:")) != -1) {
		switch (c) {
		case 'd':
			digits = strtoul(optarg, &endptr, 10);
			if (*endptr) {
				usage(argv[0]);
			}
			break;
		}
	}

	if (optind < argc) {
		usage(argv[0]);
	}

	unsigned char seed[16];
	ssize_t n = get_random_bytes(seed, sizeof(seed));
	assert(n == sizeof(seed));

	char seedhex[33];
	enchex(seed, sizeof(seed), seedhex, sizeof(seedhex));

	printf("%s:%d:%d:%d:%s\n", ALGORITHM,
		ITERATIONS, MEMORY, digits * 2, seedhex);
	return 0;
}
