From fcb0a17768142fbd660aae6a24015b67522b015a Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 4 Jan 2021 21:07:18 +0100 Subject: utilities: hash_pass: Password hashing utility (doesn't work yet) It nicely gives a hash, but the server doesn't accept the corresponding password afterwards. Probably did something stupid. --- utilities/.gitignore | 1 + utilities/Makefile | 20 ++++++++++++++++++ utilities/hash_pass.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 utilities/.gitignore create mode 100644 utilities/Makefile create mode 100644 utilities/hash_pass.c (limited to 'utilities') 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 +#include +#include +#include +#include +#include +#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"); + } + } +} -- cgit v1.2.3-70-g09d2