Fix several warnings that appear in gcc 4.3.2.
[wvstreams.git] / utils / strcrypt.cc
blob3a2170a18771015ae1f4835e92e8ad1a6e213f4c
1 #include "strutils.h"
2 #ifndef MACOS
3 #include <crypt.h>
4 #endif
5 #include <unistd.h>
6 #include <stdlib.h>
8 /**.
9 * Before calling this function, you should call srandom().
10 * When 2 identical strings are encrypted, they will not return the same
11 * encryption. Also, str does not need to be less than 8 chars as UNIX crypt
12 * says, although it only works on the first 8 characters.
14 WvString passwd_crypt(const char *str)
16 static char saltchars[] =
17 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
18 char salt[3], *result;
20 salt[0] = saltchars[random() % (sizeof(saltchars) - 1)];
21 salt[1] = saltchars[random() % (sizeof(saltchars) - 1)];
22 salt[2] = 0;
24 result = crypt(str, salt);
25 if (!result)
26 return "*";
28 WvString s(result);
29 return s;
32 /**.
33 * Before calling this function, you should call srandom().
34 * When 2 identical strings are encrypted, they will not return the same
35 * encryption. Also, str does not need to be less than 8 chars as we're
36 * using the glibc md5 algorithm.
38 WvString passwd_md5(const char *str)
40 static char saltchars[] =
41 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
42 char salt[12], *result;
44 salt[0] = '$';
45 salt[1] = '1';
46 salt[2] = '$';
48 for (int i = 3; i < 11; ++i)
49 salt[i] = saltchars[random() % (sizeof(saltchars) - 1)];
51 salt[11] = 0;
53 result = crypt(str, salt);
54 if (!result)
55 return "*";
57 WvString s(result);
58 return s;