diff options
-rw-r--r-- | utilities/.gitignore | 1 | ||||
-rw-r--r-- | utilities/Makefile | 20 | ||||
-rw-r--r-- | utilities/hash_pass.c | 57 |
3 files changed, 78 insertions, 0 deletions
diff --git a/utilities/.gitignore b/utilities/.gitignore new file mode 100644 index 0000000..41ea945 --- /dev/null +++ b/utilities/.gitignore @@ -0,0 +1 @@ +hash_pass diff --git a/utilities/Makefile b/utilities/Makefile new file mode 100644 index 0000000..684054a --- /dev/null +++ b/utilities/Makefile @@ -0,0 +1,20 @@ +CC = gcc +CFLAGS = -Wall -Wextra -std=c11 -g -O2 -D_DEFAULT_SOURCE +LDFLAGS = +CFLAGS += $(shell pkg-config --cflags libsodium) +LDFLAGS += $(shell pkg-config --libs libsodium) + +SOURCES = $(wildcard *.c) +TARGETS = $(SOURCES:.c=) + + +.PHONY: all clean + +all: $(TARGETS) + +clean: + rm -f $(TARGETS) + + +$(TARGETS): %: %.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) diff --git a/utilities/hash_pass.c b/utilities/hash_pass.c new file mode 100644 index 0000000..fdabbb6 --- /dev/null +++ b/utilities/hash_pass.c @@ -0,0 +1,57 @@ +#include <stdio.h> +#include <stdbool.h> +#include <string.h> +#include <unistd.h> +#include <termios.h> +#include <sodium.h> +#include "../hashing_settings.h" + + +int main() { + bool echo_success = false; + + struct termios tios, tios_orig; + int ret = tcgetattr(0, &tios_orig); + if (ret == 0) { + memcpy(&tios, &tios_orig, sizeof(struct termios)); + tios.c_lflag &= ~ECHO; + ret = tcsetattr(0, TCSANOW, &tios); + if (ret == 0) echo_success = true; + } + + if (!echo_success && isatty(0)) { + fprintf(stderr, "WARNING: Cannot disable ECHO on stdin\n"); + } + + printf("Password: "); + fflush(stdout); + + char *password = NULL; + size_t passlen = 0; + ssize_t nr = getline(&password, &passlen, stdin); + if (nr > 0) { + printf("\n"); + + if (passlen > 0) password[--passlen] = '\0'; + + char result[crypto_pwhash_STRBYTES + 1]; + + ret = crypto_pwhash_str( + result, password, passlen, + PASSHASH_OPSLIMIT, PASSHASH_MEMLIMIT); + if (ret == 0) { + result[crypto_pwhash_STRBYTES] = '\0'; + + printf("Hashed: %s\n", result); + } + + sodium_memzero(password, passlen); + } + + if (echo_success) { + ret = tcsetattr(0, TCSANOW, &tios_orig); + if (ret != 0) { + fprintf(stderr, "WARNING: Cannot reset original termios on stdin\n"); + } + } +} |