aboutsummaryrefslogtreecommitdiffstats
path: root/libsecauth_client_hash.c
blob: 66bc8ac18f2cb758b6935f286c7250f020954409 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "libsecauth.h"

#include <crypt.h>
#include <string.h>


char *
libsecauth_client_hash(const struct libsecauth_spec *spec, const char *password)
{
	struct crypt_data hashbuf[2];
	const char *hash = password;
	uint32_t rounds;
	size_t i;

	memset(hashbuf, 0, sizeof(hashbuf));

	if (spec->prehash && *spec->prehash) {
		hash = crypt_r(password, spec->prehash, &hashbuf[1]);
		if (!hash)
			return NULL;
	}

	for (i = 0, rounds = spec->client_rounds; rounds--; i ^= 1) {
		hash = crypt_r(hash, spec->xferhash, &hashbuf[i]);
		if (!hash)
			return NULL;
	}

	return strdup(hash);
}