xref: /haiku/src/kits/mail/crypt.cpp (revision f7215ac853ab5fda385dffd3e3dc0e1f74662ce9)
1*f7215ac8SNathan Whitehorn /* crypt - simple encryption algorithm used for passwords
2*f7215ac8SNathan Whitehorn **
3*f7215ac8SNathan Whitehorn ** Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4*f7215ac8SNathan Whitehorn */
5*f7215ac8SNathan Whitehorn 
6*f7215ac8SNathan Whitehorn 
7*f7215ac8SNathan Whitehorn #include <Message.h>
8*f7215ac8SNathan Whitehorn 
9*f7215ac8SNathan Whitehorn #include <string.h>
10*f7215ac8SNathan Whitehorn #include <crypt.h>
11*f7215ac8SNathan Whitehorn 
12*f7215ac8SNathan Whitehorn 
13*f7215ac8SNathan Whitehorn static const char key[PASSWORD_LENGTH + 1] = "Dr. Zoidberg Enterprises, BeMail";
14*f7215ac8SNathan Whitehorn 
15*f7215ac8SNathan Whitehorn 
16*f7215ac8SNathan Whitehorn _EXPORT char *get_passwd(BMessage *msg,const char *name)
17*f7215ac8SNathan Whitehorn {
18*f7215ac8SNathan Whitehorn 	char *encryptedPassword;
19*f7215ac8SNathan Whitehorn 	ssize_t length;
20*f7215ac8SNathan Whitehorn 	if (msg->FindData(name,B_RAW_TYPE,(const void **)&encryptedPassword,&length) < B_OK || !encryptedPassword || length == 0)
21*f7215ac8SNathan Whitehorn 		return NULL;
22*f7215ac8SNathan Whitehorn 
23*f7215ac8SNathan Whitehorn 	char *buffer = new char[length];
24*f7215ac8SNathan Whitehorn 	passwd_crypt(encryptedPassword,buffer,length);
25*f7215ac8SNathan Whitehorn 
26*f7215ac8SNathan Whitehorn 	return buffer;
27*f7215ac8SNathan Whitehorn }
28*f7215ac8SNathan Whitehorn 
29*f7215ac8SNathan Whitehorn 
30*f7215ac8SNathan Whitehorn _EXPORT bool set_passwd(BMessage *msg,const char *name,const char *password)
31*f7215ac8SNathan Whitehorn {
32*f7215ac8SNathan Whitehorn 	if (!password)
33*f7215ac8SNathan Whitehorn 		return false;
34*f7215ac8SNathan Whitehorn 
35*f7215ac8SNathan Whitehorn 	ssize_t length = strlen(password) + 1;
36*f7215ac8SNathan Whitehorn 	char *buffer = new char[length];
37*f7215ac8SNathan Whitehorn 	passwd_crypt((char *)password,buffer,length);
38*f7215ac8SNathan Whitehorn 
39*f7215ac8SNathan Whitehorn 	msg->RemoveName(name);
40*f7215ac8SNathan Whitehorn 	status_t status = msg->AddData(name,B_RAW_TYPE,buffer,length,false);
41*f7215ac8SNathan Whitehorn 
42*f7215ac8SNathan Whitehorn 	delete [] buffer;
43*f7215ac8SNathan Whitehorn 	return (status >= B_OK);
44*f7215ac8SNathan Whitehorn }
45*f7215ac8SNathan Whitehorn 
46*f7215ac8SNathan Whitehorn 
47*f7215ac8SNathan Whitehorn _EXPORT void passwd_crypt(char *in,char *out,int length)
48*f7215ac8SNathan Whitehorn {
49*f7215ac8SNathan Whitehorn 	int i;
50*f7215ac8SNathan Whitehorn 
51*f7215ac8SNathan Whitehorn 	memcpy(out,in,length);
52*f7215ac8SNathan Whitehorn 	if (length > PASSWORD_LENGTH)
53*f7215ac8SNathan Whitehorn 		length = PASSWORD_LENGTH;
54*f7215ac8SNathan Whitehorn 
55*f7215ac8SNathan Whitehorn 	for (i = 0;i < length;i++)
56*f7215ac8SNathan Whitehorn 		out[i] ^= key[i];
57*f7215ac8SNathan Whitehorn }
58*f7215ac8SNathan Whitehorn 
59