aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2021-01-04 21:07:18 +0100
committerTom Smeding <tom.smeding@gmail.com>2021-01-04 21:07:18 +0100
commitfcb0a17768142fbd660aae6a24015b67522b015a (patch)
tree81eb85a72734497020197e8c3c3d52a4edaaed08
parent32566e4d640e354adaa83cf9b7240bfc2cdd83e3 (diff)
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.
-rw-r--r--utilities/.gitignore1
-rw-r--r--utilities/Makefile20
-rw-r--r--utilities/hash_pass.c57
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");
+ }
+ }
+}