vfs: check userland buffers before reading them.
[haiku.git] / src / kits / mail / crypt.cpp
blobbc56c90aba16622fa76bc786b1d597e1d0b14d9a
1 /* crypt - simple encryption algorithm used for passwords
2 **
3 ** Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4 */
7 #include <Message.h>
9 #include <string.h>
10 #include <crypt.h>
13 static const char key[PASSWORD_LENGTH + 1] = "Dr. Zoidberg Enterprises, BeMail";
16 _EXPORT char *get_passwd(const BMessage *msg,const char *name)
18 char *encryptedPassword;
19 ssize_t length;
20 if (msg->FindData(name,B_RAW_TYPE,(const void **)&encryptedPassword,&length) < B_OK || !encryptedPassword || length == 0)
21 return NULL;
23 char *buffer = new char[length];
24 passwd_crypt(encryptedPassword,buffer,length);
26 return buffer;
30 _EXPORT bool set_passwd(BMessage *msg,const char *name,const char *password)
32 if (!password)
33 return false;
35 ssize_t length = strlen(password) + 1;
36 char *buffer = new char[length];
37 passwd_crypt((char *)password,buffer,length);
39 msg->RemoveName(name);
40 status_t status = msg->AddData(name,B_RAW_TYPE,buffer,length,false);
42 delete [] buffer;
43 return (status >= B_OK);
47 _EXPORT void passwd_crypt(char *in,char *out,int length)
49 int i;
51 memcpy(out,in,length);
52 if (length > PASSWORD_LENGTH)
53 length = PASSWORD_LENGTH;
55 for (i = 0;i < length;i++)
56 out[i] ^= key[i];