2
+----------------------------------------------------------------------+
4
+----------------------------------------------------------------------+
5
| Copyright (c) 1997-2004 The PHP Group |
6
+----------------------------------------------------------------------+
7
| This source file is subject to version 3.0 of the PHP license, |
8
| that is bundled with this package in the file LICENSE, and is |
9
| available through the world-wide-web at the following url: |
10
| http://www.php.net/license/3_0.txt. |
11
| If you did not receive a copy of the PHP license and are unable to |
12
| obtain it through the world-wide-web, please send a note to |
13
| license@php.net so we can mail you a copy immediately. |
14
+----------------------------------------------------------------------+
15
| Authors: Stig Bakken <ssb@php.net> |
16
| Zeev Suraski <zeev@zend.com> |
17
| Rasmus Lerdorf <rasmus@php.net> |
18
+----------------------------------------------------------------------+
20
/* $Id: crypt.c,v 1.61 2004/02/12 19:05:41 ssb Exp $ */
46
extern char *crypt(char *__key, char *__salt);
50
#include "php_crypt.h"
54
The capabilities of the crypt() function is determined by the test programs
55
run by configure from aclocal.m4. They will set PHP_STD_DES_CRYPT,
56
PHP_EXT_DES_CRYPT, PHP_MD5_CRYPT and PHP_BLOWFISH_CRYPT as appropriate
57
for the target platform
60
#define PHP_MAX_SALT_LEN 2
64
#undef PHP_MAX_SALT_LEN
65
#define PHP_MAX_SALT_LEN 9
69
#undef PHP_MAX_SALT_LEN
70
#define PHP_MAX_SALT_LEN 12
73
#if PHP_BLOWFISH_CRYPT
74
#undef PHP_MAX_SALT_LEN
75
#define PHP_MAX_SALT_LEN 60
79
* If the configure-time checks fail, we provide DES.
80
* XXX: This is a hack. Fix the real problem
83
#ifndef PHP_MAX_SALT_LEN
84
#define PHP_MAX_SALT_LEN 2
85
#undef PHP_STD_DES_CRYPT
86
#define PHP_STD_DES_CRYPT 1
90
#define PHP_CRYPT_RAND php_rand(TSRMLS_C)
92
PHP_MINIT_FUNCTION(crypt)
94
REGISTER_LONG_CONSTANT("CRYPT_SALT_LENGTH", PHP_MAX_SALT_LEN, CONST_CS | CONST_PERSISTENT);
95
REGISTER_LONG_CONSTANT("CRYPT_STD_DES", PHP_STD_DES_CRYPT, CONST_CS | CONST_PERSISTENT);
96
REGISTER_LONG_CONSTANT("CRYPT_EXT_DES", PHP_EXT_DES_CRYPT, CONST_CS | CONST_PERSISTENT);
97
REGISTER_LONG_CONSTANT("CRYPT_MD5", PHP_MD5_CRYPT, CONST_CS | CONST_PERSISTENT);
98
REGISTER_LONG_CONSTANT("CRYPT_BLOWFISH", PHP_BLOWFISH_CRYPT, CONST_CS | CONST_PERSISTENT);
104
static unsigned char itoa64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
106
static void php_to64(char *s, long v, int n)
109
*s++ = itoa64[v&0x3f];
114
/* {{{ proto string crypt(string str [, string salt])
118
char salt[PHP_MAX_SALT_LEN+1];
119
char *str, *salt_in = NULL;
120
int str_len, salt_in_len;
122
salt[0]=salt[PHP_MAX_SALT_LEN]='\0';
123
/* This will produce suitable results if people depend on DES-encryption
124
available (passing always 2-character salt). At least for glibc6.1 */
125
memset(&salt[1], '$', PHP_MAX_SALT_LEN-1);
127
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &str, &str_len,
128
&salt_in, &salt_in_len) == FAILURE) {
133
memcpy(salt, salt_in, MIN(PHP_MAX_SALT_LEN, salt_in_len));
136
/* The automatic salt generation only covers standard DES and md5-crypt */
140
php_to64(&salt[3], PHP_CRYPT_RAND, 4);
141
php_to64(&salt[7], PHP_CRYPT_RAND, 4);
142
strcpy(&salt[11], "$");
143
#elif PHP_STD_DES_CRYPT
144
php_to64(&salt[0], PHP_CRYPT_RAND, 2);
149
RETVAL_STRING(crypt(str, salt), 1);
159
* vim600: sw=4 ts=4 fdm=marker