mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-25 14:35:04 +00:00
New function hash_password() and check_password().
This commit is contained in:
parent
c88d4f1e15
commit
8be433c9b6
3 changed files with 39 additions and 0 deletions
3
main.c
3
main.c
|
@ -16,5 +16,8 @@ int main(int argc, char *argv[])
|
||||||
snac_open(&snac, "mike");
|
snac_open(&snac, "mike");
|
||||||
snac_log(&snac, xs_str_new("ok"));
|
snac_log(&snac, xs_str_new("ok"));
|
||||||
|
|
||||||
|
char *passwd = xs_dict_get(snac.config, "passwd");
|
||||||
|
printf("%d\n", check_password("mike", "1234", passwd));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
33
snac.c
33
snac.c
|
@ -90,3 +90,36 @@ void snac_debug(snac *snac, int level, d_char *str)
|
||||||
fprintf(stderr, "%s [%s] %s\n", tm, snac->uid, msg);
|
fprintf(stderr, "%s [%s] %s\n", tm, snac->uid, msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
d_char *hash_password(char *uid, char *passwd, char *nonce)
|
||||||
|
/* hashes a password */
|
||||||
|
{
|
||||||
|
xs *d_nonce = NULL;
|
||||||
|
xs *combi;
|
||||||
|
xs *hash;
|
||||||
|
|
||||||
|
if (nonce == NULL)
|
||||||
|
nonce = d_nonce = xs_fmt("%08x", random());
|
||||||
|
|
||||||
|
combi = xs_fmt("%s:%s:%s", nonce, uid, passwd);
|
||||||
|
hash = xs_sha1_hex(combi, strlen(combi));
|
||||||
|
|
||||||
|
return xs_fmt("%s:%s", nonce, hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int check_password(char *uid, char *passwd, char *hash)
|
||||||
|
/* checks a password */
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
xs *spl = xs_splitn(hash, ":", 1);
|
||||||
|
|
||||||
|
if (xs_list_len(spl) == 2) {
|
||||||
|
xs *n_hash = hash_password(uid, passwd, xs_list_get(spl, 0));
|
||||||
|
|
||||||
|
ret = (strcmp(hash, n_hash) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
3
snac.h
3
snac.h
|
@ -33,3 +33,6 @@ void snac_debug(snac *snac, int level, d_char *str);
|
||||||
#define snac_log(snac, str) snac_debug(snac, 0, str)
|
#define snac_log(snac, str) snac_debug(snac, 0, str)
|
||||||
|
|
||||||
int validate_uid(char *uid);
|
int validate_uid(char *uid);
|
||||||
|
|
||||||
|
d_char *hash_password(char *uid, char *passwd, char *nonce);
|
||||||
|
int check_password(char *uid, char *passwd, char *hash);
|
||||||
|
|
Loading…
Reference in a new issue