source: crypting.c @ 5d3b4e8

1.2.1-2
Last change on this file since 5d3b4e8 was 69ac78c, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-02-04T23:54:08Z

Added bogus G_GNUC_MALLOC to restore GLib 2.4 compatibility (hopefully).

  • Property mode set to 100644
File size: 4.5 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Sjoerd Hemminga and others                     *
5  \********************************************************************/
6
7/* A little bit of encryption for the users' passwords                  */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26/* [WvG] This file can also be compiled into a stand-alone program
27   which can encode/decode BitlBee account files. The main() will be
28   included if CRYPTING_MAIN is defined. Or just do "make decode" and
29   the programs will be built. */
30
31#include <bitlbee.h>
32#include "md5.h"
33#include "crypting.h"
34
35/*\
36 * [SH] Do _not_ call this if it's not entirely sure that it will not cause
37 * harm to another users file, since this does not check the password for
38 * correctness.
39\*/
40
41int checkpass (const char *pass, const char *md5sum)
42{
43        md5_state_t md5state;
44        md5_byte_t digest[16];
45        int i, j;
46        char digits[3];
47       
48        md5_init (&md5state);
49        md5_append (&md5state, (unsigned char *)pass, strlen (pass));
50        md5_finish (&md5state, digest);
51       
52        for (i = 0, j = 0; i < 16; i++, j += 2) {
53                /* Check password for correctness */
54                g_snprintf (digits, sizeof (digits), "%02x\n", digest[i]);
55               
56                if (digits[0] != md5sum[j]) return (-1);
57                if (digits[1] != md5sum[j + 1]) return (-1);
58        }
59
60        return( 0 );
61}
62
63
64char *hashpass (const char *password)
65{
66        md5_state_t md5state;
67        md5_byte_t digest[16];
68        int i;
69        char digits[3];
70        char *rv;
71       
72        if (password == NULL) return (NULL);
73       
74        rv = g_new0 (char, 33);
75       
76        md5_init (&md5state);
77        md5_append (&md5state, (const unsigned char *)password, strlen (password));
78        md5_finish (&md5state, digest);
79       
80        for (i = 0; i < 16; i++) {
81                /* Build a hash of the pass */
82                g_snprintf (digits, sizeof (digits), "%02x", digest[i]);
83                strcat (rv, digits);
84        }
85       
86        return (rv);
87}
88
89char *obfucrypt (char *line, const char *password) 
90{
91        int i, j;
92        char *rv;
93       
94        if (password == NULL) return (NULL);
95       
96        rv = g_new0 (char, strlen (line) + 1);
97       
98        i = j = 0;
99        while (*line) {
100                /* Encrypt/obfuscate the line, using the password */
101                if (*(signed char*)line < 0) *line = - (*line);
102               
103                rv[j] = *line + password[i]; /* Overflow intended */
104               
105                line++;
106                if (!password[++i]) i = 0;
107                j++;
108        }
109       
110        return (rv);
111}
112
113char *deobfucrypt (char *line, const char *password) 
114{
115        int i, j;
116        char *rv;
117       
118        if (password == NULL) return (NULL);
119       
120        rv = g_new0 (char, strlen (line) + 1);
121       
122        i = j = 0;
123        while (*line) {
124                /* Decrypt/deobfuscate the line, using the pass */
125                rv[j] = *line - password[i]; /* Overflow intended */
126               
127                line++;
128                if (!password[++i]) i = 0;
129                j++;
130        }
131       
132        return (rv);
133}
134
135#ifdef CRYPTING_MAIN
136
137/* A little main() function for people who want a stand-alone program to
138   encode/decode BitlCrypted files. */
139
140int main( int argc, char *argv[] )
141{
142        char *hash, *action, line[256];
143        char* (*func)( char *, const char * );
144       
145        if( argc < 2 )
146        {
147                fprintf( stderr, "Usage: %s <password>\n\n"
148                                 "Reads from stdin, writes to stdout.\n"
149                                 "Call as \"encode\" to encode, \"decode\" to decode.\n", argv[0] );
150                return( 1 );
151        }
152       
153        hash = hashpass( argv[1] );
154        action = argv[0] + strlen( argv[0] ) - strlen( "encode" );
155       
156        if( strcmp( action, "encode" ) == 0 )
157        {
158                fwrite( hash, 32, 1, stdout );
159                func = obfucrypt;
160        }
161        else if( strcmp( action, "decode" ) == 0 )
162        {
163                char hash2[32];
164               
165                fread( hash2, 32, 1, stdin );
166                if( memcmp( hash, hash2, 32 ) != 0 )
167                {
168                        fprintf( stderr, "Passwords don't match. Can't decode.\n" );
169                        return( 1 );
170                }
171                func = deobfucrypt;
172        }
173        else
174        {
175                return( main( 0, NULL ) );
176        }
177       
178        while( fscanf( stdin, "%[^\n]255s", line ) > 0 )
179        {
180                char *out;
181               
182                /* Flush the newline */
183                fgetc( stdin );
184               
185                out = func( line, argv[1] );
186                printf( "%s\n", out );
187                g_free( out );
188        }
189       
190        return( 0 );
191}
192
193#endif
Note: See TracBrowser for help on using the repository browser.