Home Backend Development PHP Tutorial 解析PHP默认的session_id生成算法

解析PHP默认的session_id生成算法

Jun 20, 2016 pm 01:00 PM
php

作为一个web程序员,我们对session肯定都不陌生,session id是我们各自在服务器上的一个唯一标志,这个id串既可以由php自动来生成,也可以由我们来赋予。你们可能和我一样,很关心php自动生成的那个id串是怎么来的,冲突的概率有多大,以及容不容易被别人计算出来,所以有了下文。

我们下载一份php5.3.6的源码,进入/ext/session目录,生成session id的函数位于session.c文件的345行,下面详细介绍一下这个函数。为了方面理解,我调整了一些代码的顺序。

PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS) /* {{{ */
{
//这几行行定义了些散列函数所需的数据,直接越过~
PHP_MD5_CTX md5_context;
PHP_SHA1_CTX sha1_context;
#if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
void *hash_context;
#endif
unsigned char *digest;
int digest_len;
int j;
char *buf, *outid;
zval **array;
zval **token;
//用来记录$_SERVER['REMOTE_ADDR']的值
char *remote_addr = NULL;
//一个timeval结构,用来记录当前的时间戳及毫秒数
struct timeval tv;
gettimeofday(&tv, NULL);
//如果可能的话,就对remote_ADDR进行赋值,用php伪代码表示便是:
//if(isset($_SERVER['REMOTE_ADDR']))
//{remote_addr = $_SERVER['REMOTE_ADDR'];}
//备注:在cli模式下是没有的~
if (
     zend_hash_find(
         &EG(symbol_table),
         "_SERVER",
         sizeof("_SERVER"),
         (void **) &array
     ) == SUCCESS
     && Z_TYPE_PP(array) == IS_ARRAY
     && zend_hash_find(
         Z_ARRVAL_PP(array),
         "REMOTE_ADDR",
         sizeof("REMOTE_ADDR"),
         (void **) &token
     ) == SUCCESS
)
{
     remote_addr = Z_STRVAL_PP(token);
}
/* maximum 15+19+19+10 bytes */
//生成所需的session id,当然后面还需要后续的处理~
//格式为:%.15s%ld%ld%0.8F,每一段的含义如下:
//%.15s    remote_addr ? remote_addr : "" 这一行很容易理解
//%ld        tv.tv_sec    当前的时间戳
//%ld        (long int)tv.tv_usec 当前毫秒数
//%0.8F    php_combined_lcg(TSRMLS_C) * 10 一个随机数
spprintf(
     &buf,
     0,
     "%.15s%ld%ld%0.8F",
     remote_addr ? remote_addr : "",
     tv.tv_sec,
     (long int)tv.tv_usec,
     php_combined_lcg(TSRMLS_C) * 10
);
//下面对buf字符串的值进行散列处理
//检测session配置中的散列函数
/*
300行:    enum{
            PS_HASH_FUNC_MD5,
            PS_HASH_FUNC_SHA1,
            PS_HASH_FUNC_OTHER
        };
812行:
PHP_INI_ENTRY("session.hash_function","0",PHP_INI_ALL,OnUpdateHashFunc)
738行:
static PHP_INI_MH(OnUpdateHashFunc)
{
    ......
    ......
    val = strtol(new_value, &endptr, 10);
    if (endptr && (*endptr == '\0'))
    {
        /* Numeric value */
         PS(hash_func) = val ? 1 : 0;
         return SUCCESS;
     }
     ......
     ......
可知PS(hash_func)的默认值为0,即PS_HASH_FUNC_MD5。
*/
switch (PS(hash_func))
{
     //如果是md5,则用md5算法对我们的buf串进行散列处理。
     case PS_HASH_FUNC_MD5:
         PHP_MD5Init(&md5_context);
         PHP_MD5Update(&md5_context, (unsigned char *) buf, strlen(buf));
         digest_len = 16;
         break;
     //如果是SHA1,则用SHA1算法对我们的buf串进行散列处理。
     case PS_HASH_FUNC_SHA1:
         PHP_SHA1Init(&sha1_context);
         PHP_SHA1Update(&sha1_context, (unsigned char *) buf, strlen(buf));
         digest_len = 20;
         break;
#if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
     case PS_HASH_FUNC_OTHER:
         if (!PS(hash_ops))
         {
             php_error_docref(
                 NULL TSRMLS_CC,
                 E_ERROR,
                 "Invalid session hash function"
             );
             efree(buf);
             return NULL;
         }
         hash_context = emalloc(PS(hash_ops)->context_size);
         PS(hash_ops)->hash_init(hash_context);
         PS(hash_ops)->hash_update(hash_context, (unsigned char *) buf, strlen(buf));
         digest_len = PS(hash_ops)->digest_size;
         break;
#endif /* HAVE_HASH_EXT */
     //如果没有散列函数,则报错,还是E_ERROR级别的,囧~
     default:
         php_error_docref(NULL TSRMLS_CC, E_ERROR, "Invalid session hash function");
         efree(buf);
         return NULL;
}
//释放buf~
//囧,那内容呢,内容已经去我们的hash_context里,比如md5_context、sha1_context。。。。。。
efree(buf);
/*
session.entropy_file 给出了一个到外部资源(文件)的路径,
该资源将在会话 ID 创建进程中被用作附加的熵值资源。
例如在许多 Unix 系统下都可以用 /dev/random 或 /dev/urandom。
session.entropy_length 指定了从上面的文件中读取的字节数。默认为 0(禁用)。
如果entropy_length这个配置大于0,则:
*/
if (PS(entropy_length) > 0)
{
#ifdef PHP_WIN32
     unsigned char rbuf[2048];
     size_t toread = PS(entropy_length);
     if (php_win32_get_random_bytes(rbuf, (size_t) toread) == SUCCESS)
     {
         switch (PS(hash_func))
         {
             case PS_HASH_FUNC_MD5:
                 PHP_MD5Update(&md5_context, rbuf, toread);
                 break;
             case PS_HASH_FUNC_SHA1:
                 PHP_SHA1Update(&sha1_context, rbuf, toread);
                 break;
# if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
             case PS_HASH_FUNC_OTHER:
                 PS(hash_ops)->hash_update(hash_context, rbuf, toread);
                 break;
# endif /* HAVE_HASH_EXT */
         }
     }
#else
     int fd;
     fd = VCWD_OPEN(PS(entropy_file), O_RDONLY);
     if (fd >= 0)
     {
         unsigned char rbuf[2048];
         int n;
         int to_read = PS(entropy_length);
         while (to_read > 0) {
             n = read(fd, rbuf, MIN(to_read, sizeof(rbuf)));
             if (n hash_update(hash_context, rbuf, n);
                     break;
#endif /* HAVE_HASH_EXT */
             }
             to_read -= n;
         }
         close(fd);
     }
//结束entropy_length>0时的逻辑
#endif
}
//还是散列计算的一部分,看来我们的hash_final(digest, hash_context);
         efree(hash_context);
         break;
#endif /* HAVE_HASH_EXT */
}
/*
session.hash_bits_per_character允许用户定义将二进制散列数据转换为可读的格式时每个字符存放多少个比特。
可能值为 '4'(0-9,a-f),'5'(0-9,a-v),以及 '6'(0-9,a-z,A-Z,"-",",")。
*/
if (PS(hash_bits_per_character) < 4
         || PS(hash_bits_per_character) > 6) {
     PS(hash_bits_per_character) = 4;
     php_error_docref(
         NULL TSRMLS_CC,
         E_WARNING,
         "The ini setting hash_bits_per_character is out of range (should be 4, 5, or 6) - using 4 for now"
     );
}
//将我们的散列后的二进制数据digest用字符串表示成可读的形式,并放置在outid字符串里
outid = emalloc((size_t)((digest_len + 2) * ((8.0f / PS(hash_bits_per_character)) + 0.5)));
j = (int) (bin_to_readable((char *)digest, digest_len, outid, (char)PS(hash_bits_per_character)) - outid);
efree(digest);
if (newlen) {
     *newlen = j;
}
//返回outid
return outid;
}
Copy after login

 


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles