mirror of
https://git.sr.ht/~sircmpwn/mkproof
synced 2024-05-28 22:16:10 +02:00
Split mkproof logic into own function
This commit is contained in:
parent
4a8e70d38c
commit
e73a6e9c66
|
@ -17,6 +17,42 @@ die(int check, char *why)
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
run_mkproof(argon2_context context, int digits)
|
||||
{
|
||||
unsigned char password[16];
|
||||
unsigned char hash[32];
|
||||
|
||||
context.out = hash;
|
||||
context.outlen = sizeof(hash);
|
||||
context.pwd = password;
|
||||
context.pwdlen = sizeof(password);
|
||||
|
||||
unsigned long nattempts = 0;
|
||||
int valid = 0;
|
||||
while (!valid) {
|
||||
if (isatty(STDERR_FILENO)) {
|
||||
fprintf(stderr, "\rAttempt %lu", ++nattempts);
|
||||
}
|
||||
|
||||
ssize_t b = get_random_bytes(password, sizeof(password));
|
||||
assert(b == sizeof(password));
|
||||
|
||||
int r = argon2id_ctx(&context);
|
||||
die(r != 0, "argon2id failed\n");
|
||||
|
||||
valid = hash_msb(hash) >= digits;
|
||||
}
|
||||
|
||||
if (isatty(STDERR_FILENO)) {
|
||||
fprintf(stderr, ": found.\nHere is your proof:\n");
|
||||
}
|
||||
|
||||
char proof[33];
|
||||
enchex(password, sizeof(password), proof, sizeof(proof));
|
||||
printf("%s\n", proof);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -53,15 +89,15 @@ main(int argc, char *argv[])
|
|||
int r = dechex(saltstr, strlen(saltstr), salt, sizeof(salt));
|
||||
die(r == -1, "Invalid challenge");
|
||||
|
||||
unsigned char password[16];
|
||||
unsigned char hash[32];
|
||||
if (isatty(STDERR_FILENO)) {
|
||||
// TODO: Provide a better estimate based on the difficulty
|
||||
fprintf(stderr, "Generating a proof of work.\n");
|
||||
fprintf(stderr, "This may take anywhere from several minutes to a few hours on slow computers.\n");
|
||||
}
|
||||
|
||||
argon2_context context = {
|
||||
.out = hash,
|
||||
.outlen = sizeof(hash),
|
||||
.salt = salt,
|
||||
.saltlen = sizeof(salt),
|
||||
.pwd = password,
|
||||
.pwdlen = sizeof(password),
|
||||
.t_cost = iters,
|
||||
.m_cost = memory,
|
||||
.lanes = 1,
|
||||
|
@ -70,34 +106,7 @@ main(int argc, char *argv[])
|
|||
.version = ARGON2_VERSION_NUMBER,
|
||||
};
|
||||
|
||||
if (isatty(STDERR_FILENO)) {
|
||||
// TODO: Provide a better estimate based on the difficulty
|
||||
fprintf(stderr, "Generating a proof of work.\n");
|
||||
fprintf(stderr, "This may take anywhere from several minutes to a few hours on slow computers.\n");
|
||||
}
|
||||
run_mkproof(context, digits);
|
||||
|
||||
unsigned long nattempts = 0;
|
||||
int valid = 0;
|
||||
while (!valid) {
|
||||
if (isatty(STDERR_FILENO)) {
|
||||
fprintf(stderr, "\rAttempt %lu", ++nattempts);
|
||||
}
|
||||
|
||||
ssize_t b = get_random_bytes(password, sizeof(password));
|
||||
assert(b == sizeof(password));
|
||||
|
||||
r = argon2id_ctx(&context);
|
||||
die(r != 0, "argon2id failed\n");
|
||||
|
||||
valid = hash_msb(hash) >= digits;
|
||||
}
|
||||
|
||||
if (isatty(STDERR_FILENO)) {
|
||||
fprintf(stderr, ": found.\nHere is your proof:\n");
|
||||
}
|
||||
|
||||
char proof[33];
|
||||
enchex(password, sizeof(password), proof, sizeof(proof));
|
||||
printf("%s\n", proof);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue