diff options
Diffstat (limited to 'utilities/hash_pass.c')
-rw-r--r-- | utilities/hash_pass.c | 57 |
1 files changed, 57 insertions, 0 deletions
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"); + } + } +} |