您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

解析PHP默认的session_id生成算法

2024/4/3 9:31:31发布22次查看
作为一个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;#endifunsigned 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;}//返回outidreturn outid;}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product