File: strcrypt.cc

package info (click to toggle)
wvstreams 4.0.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,420 kB
  • ctags: 6,518
  • sloc: cpp: 52,544; sh: 5,770; ansic: 810; makefile: 461; tcl: 114; perl: 18
file content (58 lines) | stat: -rw-r--r-- 1,458 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "strutils.h"
#include <crypt.h>

#include <unistd.h>
#include <stdlib.h>

/**.
 * Before calling this function, you should call srandom().
 * When 2 identical strings are encrypted, they will not return the same
 * encryption. Also, str does not need to be less than 8 chars as UNIX crypt
 * says, although it only works on the first 8 characters.
 */
WvString passwd_crypt(const char *str)
{
    static char saltchars[] =
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
    char salt[3], *result;

    salt[0] = saltchars[random() % (sizeof(saltchars) - 1)];
    salt[1] = saltchars[random() % (sizeof(saltchars) - 1)];
    salt[2] = 0;

    result = crypt(str, salt);
    if (!result)
	return "*";

    WvString s(result);
    return s;
}

/**.
 * Before calling this function, you should call srandom().
 * When 2 identical strings are encrypted, they will not return the same
 * encryption. Also, str does not need to be less than 8 chars as we're
 * using the glibc md5 algorithm.
 */
WvString passwd_md5(const char *str)
{
    static char saltchars[] =
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
    char salt[12], *result;

    salt[0] = '$';
    salt[1] = '1';
    salt[2] = '$';

    for (int i = 3; i < 11; ++i)
	salt[i] = saltchars[random() % (sizeof(saltchars) - 1)];

    salt[11] = 0;

    result = crypt(str, salt);
    if (!result)
	return "*";

    WvString s(result);
    return s;
}