mirror of
https://git.sr.ht/~sircmpwn/mkproof
synced 2024-05-28 01:26:09 +02:00
Use multiple processes to find proof
This commit is contained in:
parent
e73a6e9c66
commit
4c1cba82b2
|
@ -1,13 +1,20 @@
|
|||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include "argon2.h"
|
||||
#include "random.h"
|
||||
#include "util.h"
|
||||
#include "proof.h"
|
||||
|
||||
|
||||
#define MAX_PIDS 512
|
||||
|
||||
static void
|
||||
die(int check, char *why)
|
||||
{
|
||||
|
@ -28,13 +35,8 @@ run_mkproof(argon2_context context, int digits)
|
|||
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));
|
||||
|
||||
|
@ -45,12 +47,13 @@ run_mkproof(argon2_context context, int digits)
|
|||
}
|
||||
|
||||
if (isatty(STDERR_FILENO)) {
|
||||
fprintf(stderr, ": found.\nHere is your proof:\n");
|
||||
fprintf(stderr, "Here is your proof:\n");
|
||||
}
|
||||
|
||||
char proof[33];
|
||||
enchex(password, sizeof(password), proof, sizeof(proof));
|
||||
printf("%s\n", proof);
|
||||
_exit(0);
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -95,18 +98,49 @@ main(int argc, char *argv[])
|
|||
fprintf(stderr, "This may take anywhere from several minutes to a few hours on slow computers.\n");
|
||||
}
|
||||
|
||||
argon2_context context = {
|
||||
.salt = salt,
|
||||
.saltlen = sizeof(salt),
|
||||
.t_cost = iters,
|
||||
.m_cost = memory,
|
||||
.lanes = 1,
|
||||
.threads = 1,
|
||||
.flags = ARGON2_DEFAULT_FLAGS,
|
||||
.version = ARGON2_VERSION_NUMBER,
|
||||
};
|
||||
#ifdef _SC_NPROCESSORS_ONLN
|
||||
long nprocs = sysconf(_SC_NPROCESSORS_ONLN) - 1;
|
||||
#else
|
||||
long nprocs = 2;
|
||||
#endif
|
||||
|
||||
run_mkproof(context, digits);
|
||||
pid_t pids[MAX_PIDS];
|
||||
assert(nprocs <= MAX_PIDS);
|
||||
|
||||
for (size_t i = 0; i < (size_t)nprocs; i++) {
|
||||
pid_t pid = fork();
|
||||
die(pid < 0, "fork()");
|
||||
|
||||
if (pid == 0) {
|
||||
argon2_context context = {
|
||||
.salt = salt,
|
||||
.saltlen = sizeof(salt),
|
||||
.t_cost = iters,
|
||||
.m_cost = memory,
|
||||
.lanes = 1,
|
||||
.threads = 1,
|
||||
.flags = ARGON2_DEFAULT_FLAGS,
|
||||
.version = ARGON2_VERSION_NUMBER,
|
||||
};
|
||||
run_mkproof(context, digits);
|
||||
// unreachable
|
||||
assert(0);
|
||||
}
|
||||
|
||||
pids[i] = pid;
|
||||
}
|
||||
|
||||
int wstatus;
|
||||
pid_t pid = wait(&wstatus);
|
||||
|
||||
for (size_t i = 0; i < (size_t)nprocs; i++) {
|
||||
if (pids[i] == pid) {
|
||||
continue;
|
||||
}
|
||||
|
||||
r = kill(pids[i], SIGTERM);
|
||||
die(r == -1, strerror(errno));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue