summaryrefslogtreecommitdiff
path: root/src/skey/md5.c
blob: de9d7a7e7f81d7309bc0a3fe1c94099556866188 (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
64
65
66
67
#include <config.h>
#include <stdlib.h>

#include <string.h>
#include <glib.h>

#include "skey.h"
#include "skeyutil.h"
#include "md5.h"

int MD5Keycrunch(char *result, const char *seed, const char *passhrase)
{
	char *buf;
	gsize len;
	GChecksum *checksum;
	guint8 digest[16];
	gsize digest_len = sizeof (digest);
	guint32 *results;

	len = strlen(seed) + strlen(passhrase);
	buf = (char *)g_try_malloc(len+1);
	if (buf == NULL)
		return -1;

	strcpy(buf, seed);
	skey_lowcase(buf);
	strcat(buf, passhrase);
	skey_sevenbit(buf);

	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum, (const guchar *) buf, len);
	g_free(buf);

	g_checksum_get_digest (checksum, digest, &digest_len);
	g_assert (digest_len == 16);

	results = (guint32 *) digest;
	results[0] ^= results[2];
	results[1] ^= results[3];

	memcpy((void *)result, (void *)results, SKEY_SIZE);

	g_checksum_free (checksum);

	return 0;
}

void MD5SKey(char *x)
{
	GChecksum *checksum;
	guint8 digest[16];
	gsize digest_len = sizeof (digest);
	guint32 *results;

	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum, (const guchar *) x, SKEY_SIZE);
	g_checksum_get_digest (checksum, digest, &digest_len);
	g_assert (digest_len == 16);

	results = (guint32 *) digest;
	results[0] ^= results[2];
	results[1] ^= results[3];

	memcpy((void *)x, (void *)results, SKEY_SIZE);

	g_checksum_free (checksum);
}