bcee3b072eb7958228f1e6f763ce8c58eb2b2443
[freeradius.git] / src / modules / rlm_mschap / smbencrypt.c
1 /*
2  * smbencrypt.c Produces LM-Password and NT-Password from
3  *              cleartext password
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * Copyright 2002  3APA3A for FreeRADIUS project
20  */
21
22 #include        "autoconf.h"
23 #include        "libradius.h"
24 #include        <stdio.h>
25 #include        <stdlib.h>
26 #include        <string.h>
27 #include        <ctype.h>
28
29 #include        "md4.h"
30
31 static const char * hex = "0123456789ABCDEF";
32
33 static void tohex (const unsigned char * src, size_t len, char *dst)
34 {
35         int i;
36         for (i=0; i<len; i++) {
37                 dst[(i*2)] = hex[(src[i] >> 4)];
38                 dst[(i*2) + 1] = hex[(src[i]&0x0F)];
39         }
40         dst[(i*2)] = 0;
41 }
42
43 static void ntpwdhash (char *szHash, const char *szPassword)
44 {
45         char szUnicodePass[513];
46         char nPasswordLen;
47         int i;
48
49         /*
50          *      NT passwords are unicode.  Convert plain text password
51          *      to unicode by inserting a zero every other byte
52          */
53         nPasswordLen = strlen(szPassword);
54         for (i = 0; i < nPasswordLen; i++) {
55                 szUnicodePass[i << 1] = szPassword[i];
56                 szUnicodePass[(i << 1) + 1] = 0;
57         }
58
59         /* Encrypt Unicode password to a 16-byte MD4 hash */
60         md4_calc(szHash, szUnicodePass, (nPasswordLen<<1) );
61 }
62
63
64
65 int main (int argc, char *argv[])
66 {
67         int i, l;
68         char password[1024];
69         char hash[16];
70         char ntpass[33];
71         char lmpass[33];
72         
73         fprintf(stderr, "LM Hash                         \tNT Hash\n");
74         fprintf(stderr, "--------------------------------\t--------------------------------\n");
75         fflush(stderr);
76         for (i = 1; i < argc; i++ ) {
77                 l = strlen(password);
78                 if (l && password[l-1] == '\n') password [l-1] = 0;
79                 lrad_lmpwdhash(argv[i], hash);
80                 tohex (hash, 16, lmpass);
81                 ntpwdhash (hash, argv[i]);
82                 tohex (hash, 16, ntpass);
83                 printf("%s\t%s\n", lmpass, ntpass);
84         }
85         return 0;
86 }