Fix compilation with old g++ 3.3.5 and debian-sarge.
[wvstreams.git] / utils / strcrypt.cc
blob476b5cab49ccbb0facd9a64529e110da46feffb4
1 #include "strutils.h"
2 #include <crypt.h>
4 #include <unistd.h>
5 #include <stdlib.h>
7 /**.
8 * Before calling this function, you should call srandom().
9 * When 2 identical strings are encrypted, they will not return the same
10 * encryption. Also, str does not need to be less than 8 chars as UNIX crypt
11 * says, although it only works on the first 8 characters.
13 WvString passwd_crypt(const char *str)
15 static char saltchars[] =
16 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
17 char salt[3], *result;
19 salt[0] = saltchars[random() % (sizeof(saltchars) - 1)];
20 salt[1] = saltchars[random() % (sizeof(saltchars) - 1)];
21 salt[2] = 0;
23 result = crypt(str, salt);
24 if (!result)
25 return "*";
27 WvString s(result);
28 return s;
31 /**.
32 * Before calling this function, you should call srandom().
33 * When 2 identical strings are encrypted, they will not return the same
34 * encryption. Also, str does not need to be less than 8 chars as we're
35 * using the glibc md5 algorithm.
37 WvString passwd_md5(const char *str)
39 static char saltchars[] =
40 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
41 char salt[12], *result;
43 salt[0] = '$';
44 salt[1] = '1';
45 salt[2] = '$';
47 for (int i = 3; i < 11; ++i)
48 salt[i] = saltchars[random() % (sizeof(saltchars) - 1)];
50 salt[11] = 0;
52 result = crypt(str, salt);
53 if (!result)
54 return "*";
56 WvString s(result);
57 return s;