-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ff9c9b2
Showing
3 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
pwhash | ||
# Ignore patterns for C | ||
# Prerequisites | ||
*.d | ||
|
||
# Object files | ||
*.o | ||
*.ko | ||
*.obj | ||
*.elf | ||
|
||
# Linker output | ||
*.ilk | ||
*.map | ||
*.exp | ||
|
||
# Precompiled Headers | ||
*.gch | ||
*.pch | ||
|
||
# Libraries | ||
*.lib | ||
*.a | ||
*.la | ||
*.lo | ||
|
||
# Shared objects (inc. Windows DLLs) | ||
*.dll | ||
*.so | ||
*.so.* | ||
*.dylib | ||
|
||
# Executables | ||
*.exe | ||
*.out | ||
*.app | ||
*.i*86 | ||
*.x86_64 | ||
*.hex | ||
|
||
# Debug files | ||
*.dSYM/ | ||
*.su | ||
*.idb | ||
*.pdb | ||
|
||
# Kernel Module Compile Results | ||
*.mod* | ||
*.cmd | ||
.tmp_versions/ | ||
modules.order | ||
Module.symvers | ||
Mkfile.old | ||
dkms.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
LDFLAGS = -lcrypt | ||
|
||
all: pwhash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#define _GNU_SOURCE | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <getopt.h> | ||
#include <string.h> | ||
#include <termios.h> | ||
|
||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
|
||
static const struct option options[] = { | ||
{"method", required_argument, NULL, 'm'}, | ||
{"rounds", required_argument, NULL, 'r'}, | ||
{0, 0, 0, 0} | ||
}; | ||
static const char *optstring = "m:r:"; | ||
|
||
static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"; | ||
|
||
static char *name; | ||
|
||
void usage(void) | ||
{ | ||
fprintf(stderr, "Usage: %s [(-m|--method) METHOD] [(-r|--rounds) ROUNDS]\n", name); | ||
fprintf(stderr, "See crypt(3) for possible values of METHOD and ROUNDS.\n"); | ||
exit(1); | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
name = argv[0]; | ||
char *method = NULL; | ||
char *rounds = NULL; | ||
int ch; | ||
while ((ch = getopt_long(argc, argv, optstring, options, NULL)) != -1) { | ||
switch (ch) { | ||
case 'm': | ||
method = strdup(optarg); | ||
break; | ||
case 'r': | ||
rounds = strdup(optarg); | ||
break; | ||
default: | ||
usage(); | ||
} | ||
} | ||
|
||
char salt[17] = {0}; | ||
int urandom = open("/dev/urandom", O_RDONLY); | ||
if (urandom == -1) { | ||
fprintf(stderr, "Couldn't open /dev/urandom!\n"); | ||
exit(1); | ||
} | ||
unsigned char buf; | ||
char *p = salt; | ||
for (int i = 0; i < 16; i++) { | ||
if (read(urandom, &buf, 1) != 1) { | ||
fprintf(stderr, "Couldn't read from /dev/urandom!\n"); | ||
exit(1); | ||
} | ||
*p = charset[buf % (sizeof(charset) - 1)]; | ||
p++; | ||
} | ||
|
||
char *crypt_salt = NULL; | ||
if (!method) { | ||
crypt_salt = salt; | ||
} else { | ||
if (rounds) { | ||
asprintf(&crypt_salt, "$%s$rounds=%s$%s$", method, rounds, salt); | ||
} else { | ||
asprintf(&crypt_salt, "$%s$%s$", method, salt); | ||
} | ||
if (!crypt_salt) { | ||
exit(1); | ||
} | ||
} | ||
|
||
printf("Password: "); | ||
fflush(stdout); | ||
struct termios t; | ||
tcgetattr(0, &t); | ||
t.c_lflag &= ~ECHO; | ||
tcsetattr(0, TCSADRAIN, &t); | ||
char *key = NULL; | ||
size_t key_len = 0; | ||
ssize_t len = getline(&key, &key_len, stdin); | ||
t.c_lflag |= ECHO; | ||
tcsetattr(0, TCSANOW, &t); | ||
|
||
printf("\n"); | ||
if (len <= 1) { | ||
fprintf(stderr, "Password is empty!\n"); | ||
exit(1); | ||
} | ||
if (key[len - 1] == '\n') { | ||
key[len - 1] = '\0'; | ||
} | ||
|
||
char *hash = crypt(key, crypt_salt); | ||
if (hash) { | ||
printf("%s\n", hash); | ||
exit(0); | ||
} | ||
return 1; | ||
} |