diff options
Diffstat (limited to 'snac.c')
| -rw-r--r-- | snac.c | 33 |
1 files changed, 33 insertions, 0 deletions
| @@ -90,3 +90,36 @@ void snac_debug(snac *snac, int level, d_char *str) | |||
| 90 | fprintf(stderr, "%s [%s] %s\n", tm, snac->uid, msg); | 90 | fprintf(stderr, "%s [%s] %s\n", tm, snac->uid, msg); |
| 91 | } | 91 | } |
| 92 | } | 92 | } |
| 93 | |||
| 94 | |||
| 95 | d_char *hash_password(char *uid, char *passwd, char *nonce) | ||
| 96 | /* hashes a password */ | ||
| 97 | { | ||
| 98 | xs *d_nonce = NULL; | ||
| 99 | xs *combi; | ||
| 100 | xs *hash; | ||
| 101 | |||
| 102 | if (nonce == NULL) | ||
| 103 | nonce = d_nonce = xs_fmt("%08x", random()); | ||
| 104 | |||
| 105 | combi = xs_fmt("%s:%s:%s", nonce, uid, passwd); | ||
| 106 | hash = xs_sha1_hex(combi, strlen(combi)); | ||
| 107 | |||
| 108 | return xs_fmt("%s:%s", nonce, hash); | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | int check_password(char *uid, char *passwd, char *hash) | ||
| 113 | /* checks a password */ | ||
| 114 | { | ||
| 115 | int ret = 0; | ||
| 116 | xs *spl = xs_splitn(hash, ":", 1); | ||
| 117 | |||
| 118 | if (xs_list_len(spl) == 2) { | ||
| 119 | xs *n_hash = hash_password(uid, passwd, xs_list_get(spl, 0)); | ||
| 120 | |||
| 121 | ret = (strcmp(hash, n_hash) == 0); | ||
| 122 | } | ||
| 123 | |||
| 124 | return ret; | ||
| 125 | } | ||