summaryrefslogtreecommitdiff
path: root/src/libdisfluid/disfluid-cache-entry-hash.h
blob: c7fd2717309be7ea6fee3891d8cdeb514697c444 (plain)
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
#ifndef DISFLUID_DISFLUID_CACHE_ENTRY_HASH_INCLUDED
# define DISFLUID_DISFLUID_CACHE_ENTRY_HASH_INCLUDED

MAYBE_UNUSED static int
hash_primary_cache_key (const char *restrict method,
			const char *restrict uri,
			const char *restrict password,
			size_t password_length,
			size_t max_hash, char *restrict hash);

# include <gnutls/gnutls.h>
# include <gnutls/crypto.h>
# include <assert.h>

# include "disfluid-init.h"

static int
hash_primary_cache_key (const char *restrict method,
			const char *restrict uri,
			const char *restrict password,
			size_t password_length,
			size_t max_hash, char *restrict hash)
{
  ensure_init ();
  gnutls_hmac_hd_t context;
  gnutls_mac_algorithm_t algo = GNUTLS_MAC_SHA256;
  int error = gnutls_hmac_init (&context, algo, password, password_length);
  if (error != 0)
    {
      return -1;
    }
  uint8_t digest[32];
  if (gnutls_hmac_get_len (algo) != sizeof (digest))
    {
      /* Unreachable. */
      assert (0);
    }
  error = gnutls_hmac (context, method, strlen (method));
  if (error == 0)
    {
      error = gnutls_hmac (context, " ", strlen (" "));
    }
  if (error == 0)
    {
      error = gnutls_hmac (context, uri, strlen (uri));
    }
  gnutls_hmac_deinit (context, digest);
  if (error != 0)
    {
      return -1;
    }
  gnutls_datum_t data = {
    .data = digest,
    .size = sizeof (digest)
  };
  if (gnutls_hex_encode (&data, hash, &max_hash) != 0)
    {
      return -2;
    }
  return 0;
}

#endif /* DISFLUID_DISFLUID_CACHE_ENTRY_HASH_INCLUDED */