-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
105 lines (103 loc) · 2.19 KB
/
test.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "./include/test.h"
int main(int argc, char* argv[])
{
struct passentry* entry;
char fname[]="passdata";
char* sw;
FILE* fh;
char* uid;
char* pass;
fh = fopen(fname,"a+");
if(fh == NULL)
{
error(0,errno,"Error occured");
}
entry = (struct passentry*)malloc(sizeof(struct passentry));
if(argc > 1)
{
sw = argv[1];
}
else
{
printf("No option supplied!\n");
free(entry);
return 1;
}
switch(sw[1])
{
case 's':
uid = argv[2];
pass = argv[3];
entry->password = pass;
entry->uid = uid;
storepass(entry,fh);
break;
case 'v':
uid = argv[2];
pass = argv[3];
entry->password = pass;
entry->uid = uid;
verifypass(entry,fh);
break;
}
fclose(fh);
free(entry);
return 0;
}
int verifypass(struct passentry* inpass,FILE* passdata)
{
char outhash[BCRYPT_HASHSIZE];
char* currline;
currline = (char*)malloc(MAXBUF);
int retval = 0;
while(fgets(currline,MAXBUF,passdata) != NULL)
{
//strip off trailing \n
currline[strnlen(currline,MAXBUF) - 1] = '\0';
if(strncmp(inpass->uid,currline,MAXBUF) == 0)
{
printf("Found match! %s\n",currline);
if(fgets(currline,MAXBUF,passdata) != NULL)
{
currline[strnlen(currline,MAXBUF) - 1] = '\0';
retval = bcrypt_hashpw(inpass->password,currline,outhash);
if(retval == 0)
{
if(strncmp(currline,outhash,MAXBUF) == 0)
{
printf("Password for %s was correct!\n",inpass->uid);
}
else
{
printf("Password for %s was INCORRECT!!\n",inpass->uid);
}
}
}
break;
}
}
free(currline);
return 1;
}
int storepass(struct passentry* inpass,FILE* passdata)
{
char salt[BCRYPT_HASHSIZE];
char hash[BCRYPT_HASHSIZE];
char passentry[MAXBUF];
int wf=3;
int retval=0;
retval = bcrypt_gensalt(wf,salt);
if(retval == 0)
{
retval = bcrypt_hashpw(inpass->password,salt,hash);
if(retval == 0)
{
retval = snprintf(passentry,MAXBUF,"%s\n%s\n---\n",inpass->uid,hash);
if(retval > 0)
{
retval = fputs(passentry,passdata);
}
}
}
return retval;
}