WvStreams
strcrypt.cc
00001 #include "strutils.h"
00002 #include <crypt.h>
00003 
00004 #include <unistd.h>
00005 #include <stdlib.h>
00006 
00013 WvString passwd_crypt(const char *str)
00014 {
00015     static char saltchars[] =
00016         "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
00017     char salt[3], *result;
00018 
00019     salt[0] = saltchars[random() % (sizeof(saltchars) - 1)];
00020     salt[1] = saltchars[random() % (sizeof(saltchars) - 1)];
00021     salt[2] = 0;
00022 
00023     result = crypt(str, salt);
00024     if (!result)
00025         return "*";
00026 
00027     WvString s(result);
00028     return s;
00029 }
00030 
00037 WvString passwd_md5(const char *str)
00038 {
00039     static char saltchars[] =
00040         "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
00041     char salt[12], *result;
00042 
00043     salt[0] = '$';
00044     salt[1] = '1';
00045     salt[2] = '$';
00046 
00047     for (int i = 3; i < 11; ++i)
00048         salt[i] = saltchars[random() % (sizeof(saltchars) - 1)];
00049 
00050     salt[11] = 0;
00051 
00052     result = crypt(str, salt);
00053     if (!result)
00054         return "*";
00055 
00056     WvString s(result);
00057     return s;
00058 }