1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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");
}
}
}
|