File: redhat-crypt-md5.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (126 lines) | stat: -rw-r--r-- 2,607 bytes parent folder | download | duplicates (13)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
** Copyright 1998 - 1999 Double Precision, Inc.
** See COPYING for distribution information.
*/

#define	MD5_INTERNAL
#include	"md5.h"
#include	<string.h>

static const char rcsid[]="$Id: redhat-crypt-md5.c,v 1.3 1999/12/06 13:22:43 mrsam Exp $";

static char base64[]=
        "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

char *md5_crypt_redhat(const char *pw, const char *salt)
{
struct MD5_CONTEXT outer_context, inner_context;
MD5_DIGEST digest;
unsigned pwl=strlen(pw);
unsigned l;
unsigned i, j;
char	*p;
static char buffer[100];

	if (*salt == '$' && salt[1] == '1' && salt[2] == '$')
		salt += 3;

	for (l=0; l<8 && salt[l] && salt[l] != '$'; l++)
		;

	md5_context_init(&inner_context);
	md5_context_hashstream(&inner_context, pw, pwl);
	md5_context_hashstream(&inner_context, salt, l);
	md5_context_hashstream(&inner_context, pw, pwl);
	md5_context_endstream(&inner_context, pwl*2+l);
	md5_context_digest(&inner_context, digest);

	md5_context_init(&outer_context);
	md5_context_hashstream(&outer_context, pw, pwl);
	md5_context_hashstream(&outer_context, "$1$", 3);
	md5_context_hashstream(&outer_context, salt, l);

	for (i=pwl; i; )
	{
		j=i;
		if (j > 16)	j=16;
		md5_context_hashstream(&outer_context, digest, j);
		i -= j;
	}

	j=pwl*2+l+3;

	for (i=pwl; i; i >>= 1)
	{
		md5_context_hashstream(&outer_context, (i & 1) ? "": pw, 1);
		++j;
	}


	md5_context_endstream(&outer_context, j);
	md5_context_digest(&outer_context, digest);

	for (i=0; i<1000; i++)
	{
		j=0;

		md5_context_init(&outer_context);
		if (i & 1)
		{
			md5_context_hashstream(&outer_context, pw, pwl);
			j += pwl;
		}
		else
		{
			md5_context_hashstream(&outer_context, digest, 16);
			j += 16;
		}
    
		if (i % 3)
		{
			md5_context_hashstream(&outer_context, salt, l);
			j += l;
		}
    
    		if (i % 7)
		{
			md5_context_hashstream(&outer_context, pw, pwl);
			j += pwl;
		}
    
		if (i & 1)
		{
			md5_context_hashstream(&outer_context, digest, 16);
			j += 16;
		}
		else
		{
			md5_context_hashstream(&outer_context, pw, pwl);
			j += pwl;
		}

		md5_context_endstream(&outer_context, j);
		md5_context_digest(&outer_context, digest);
	}

	strcpy(buffer, "$1$");
	strncat(buffer, salt, l);
	strcat(buffer, "$");

	p=buffer+strlen(buffer);
	for (i=0; i<5; i++)
	{
	unsigned char *d=digest;

		j= (d[i] << 16) | (d[i+6] << 8) | d[i == 4 ? 5:12+i];
		*p++= base64[j & 63] ; j=j >> 6;
		*p++= base64[j & 63] ; j=j >> 6;
		*p++= base64[j & 63] ; j=j >> 6;
		*p++= base64[j & 63];
	}
	j=digest[11];
	*p++ = base64[j & 63]; j=j >> 6;
	*p++ = base64[j & 63];
	*p=0;
	return (buffer);
}