Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gteissier
GitHub Repository: gteissier/erl-matter
Path: blob/master/erldp.c
271 views
1
#include <stdio.h>
2
#include <string.h>
3
4
#include "erldp.h"
5
6
#include <nettle/md5.h>
7
#include <nettle/base16.h>
8
9
10
static uint64_t next_random(uint64_t x);
11
12
void create_cookie(uint64_t seed, char *cookie, size_t size) {
13
uint64_t x;
14
int i;
15
16
x = seed;
17
18
for (i = size-1; i >= 0; i--) {
19
x = next_random(x);
20
cookie[i] = 'A' + ((26*x) / 0x1000000000);
21
}
22
}
23
24
static uint64_t next_random(uint64_t x) {
25
uint64_t ret;
26
ret = (x * 17059465ULL + 1) & 0xfffffffff;
27
return ret;
28
}
29
30
void compute_response(size_t cookie_size, const char *cookie,
31
uint32_t challenge, uint8_t *digest) {
32
struct md5_ctx ctx;
33
char challenge_s[256];
34
35
memset(challenge_s, 0, sizeof(challenge_s));
36
snprintf(challenge_s, sizeof(challenge_s)-1, "%u", challenge);
37
38
md5_init(&ctx);
39
40
md5_update(&ctx, cookie_size, (uint8_t *) cookie);
41
md5_update(&ctx, strlen(challenge_s), (uint8_t *) challenge_s);
42
43
md5_digest(&ctx, 16, digest);
44
}
45
46