source: crypting.c @ 55b1e69

Last change on this file since 55b1e69 was 7c9db24, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-08T02:10:41Z

Replaced obsolete (useless for the current .xml files) encode/decode tools
with a few command-line options to the BitlBee binary. Type "bitlbee -x"
for more info.

  • Property mode set to 100644
File size: 3.4 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}
Note: See TracBrowser for help on using the repository browser.