2 ** Copyright 1998 - 1999 Double Precision, Inc.
3 ** See COPYING for distribution information.
10 static const char rcsid
[]="$Id: redhat-crypt-md5.c,v 1.3 1999/12/06 13:22:43 mrsam Exp $";
13 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
15 char *md5_crypt_redhat(const char *pw
, const char *salt
)
17 struct MD5_CONTEXT outer_context
, inner_context
;
19 unsigned pwl
=strlen(pw
);
23 static char buffer
[100];
25 if (*salt
== '$' && salt
[1] == '1' && salt
[2] == '$')
28 for (l
=0; l
<8 && salt
[l
] && salt
[l
] != '$'; l
++)
31 md5_context_init(&inner_context
);
32 md5_context_hashstream(&inner_context
, pw
, pwl
);
33 md5_context_hashstream(&inner_context
, salt
, l
);
34 md5_context_hashstream(&inner_context
, pw
, pwl
);
35 md5_context_endstream(&inner_context
, pwl
*2+l
);
36 md5_context_digest(&inner_context
, digest
);
38 md5_context_init(&outer_context
);
39 md5_context_hashstream(&outer_context
, pw
, pwl
);
40 md5_context_hashstream(&outer_context
, "$1$", 3);
41 md5_context_hashstream(&outer_context
, salt
, l
);
47 md5_context_hashstream(&outer_context
, digest
, j
);
53 for (i
=pwl
; i
; i
>>= 1)
55 md5_context_hashstream(&outer_context
, (i
& 1) ? "": pw
, 1);
60 md5_context_endstream(&outer_context
, j
);
61 md5_context_digest(&outer_context
, digest
);
63 for (i
=0; i
<1000; i
++)
67 md5_context_init(&outer_context
);
70 md5_context_hashstream(&outer_context
, pw
, pwl
);
75 md5_context_hashstream(&outer_context
, digest
, 16);
81 md5_context_hashstream(&outer_context
, salt
, l
);
87 md5_context_hashstream(&outer_context
, pw
, pwl
);
93 md5_context_hashstream(&outer_context
, digest
, 16);
98 md5_context_hashstream(&outer_context
, pw
, pwl
);
102 md5_context_endstream(&outer_context
, j
);
103 md5_context_digest(&outer_context
, digest
);
106 strcpy(buffer
, "$1$");
107 strncat(buffer
, salt
, l
);
110 p
=buffer
+strlen(buffer
);
113 unsigned char *d
=digest
;
115 j
= (d
[i
] << 16) | (d
[i
+6] << 8) | d
[i
== 4 ? 5:12+i
];
116 *p
++= base64
[j
& 63] ; j
=j
>> 6;
117 *p
++= base64
[j
& 63] ; j
=j
>> 6;
118 *p
++= base64
[j
& 63] ; j
=j
>> 6;
119 *p
++= base64
[j
& 63];
122 *p
++ = base64
[j
& 63]; j
=j
>> 6;
123 *p
++ = base64
[j
& 63];