File: strcrypt.cc

package info (click to toggle)
wvstreams 4.6.1-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,420 kB
  • sloc: cpp: 64,196; ansic: 4,154; sh: 4,025; makefile: 546; perl: 402
file content (59 lines) | stat: -rw-r--r-- 1,480 bytes parent folder | download | duplicates (8)
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
59
#include "strutils.h"
#ifndef MACOS
  #include <crypt.h>
#endif
#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;
}