📄 mcrypt.c
字号:
/* {{{ proto string mcrypt_generic(resource td, string data) This function encrypts the plaintext */PHP_FUNCTION(mcrypt_generic){ zval **data, **mcryptind; php_mcrypt *pm; int argc; unsigned char* data_s; int block_size, data_size; argc = ZEND_NUM_ARGS(); MCRYPT_CHECK_PARAM_COUNT_EX (2,2) zend_get_parameters_ex(2, &mcryptind, &data); ZEND_FETCH_RESOURCE(pm, php_mcrypt *, mcryptind, -1, "MCrypt", le_mcrypt); PHP_MCRYPT_INIT_CHECK convert_to_string_ex(data); if (Z_STRLEN_PP(data) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "An empty string was passed"); RETURN_FALSE } /* Check blocksize */ if (mcrypt_enc_is_block_mode(pm->td) == 1) { /* It's a block algorithm */ block_size = mcrypt_enc_get_block_size(pm->td); data_size = (((Z_STRLEN_PP(data) - 1) / block_size) + 1) * block_size; data_s = emalloc (data_size + 1); memset (data_s, 0, data_size); memcpy (data_s, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); } else { /* It's not a block algorithm */ data_size = Z_STRLEN_PP(data); data_s = emalloc(data_size + 1); memset(data_s, 0, data_size); memcpy(data_s, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); } mcrypt_generic(pm->td, data_s, data_size); data_s[data_size] = '\0'; RETVAL_STRINGL (data_s, data_size, 1); efree (data_s);}/* }}} *//* {{{ proto string mdecrypt_generic(resource td, string data) This function decrypts the plaintext */PHP_FUNCTION(mdecrypt_generic){ zval **data, **mcryptind; php_mcrypt *pm; int argc; char* data_s; int block_size, data_size; argc = ZEND_NUM_ARGS(); MCRYPT_CHECK_PARAM_COUNT_EX (2,2) zend_get_parameters_ex(2, &mcryptind, &data); ZEND_FETCH_RESOURCE(pm, php_mcrypt * , mcryptind, -1, "MCrypt", le_mcrypt); PHP_MCRYPT_INIT_CHECK convert_to_string_ex(data); if (Z_STRLEN_PP(data) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "An empty string was passed"); RETURN_FALSE } /* Check blocksize */ if (mcrypt_enc_is_block_mode(pm->td) == 1) { /* It's a block algorithm */ block_size = mcrypt_enc_get_block_size(pm->td); data_size = (((Z_STRLEN_PP(data) - 1) / block_size) + 1) * block_size; data_s = emalloc (data_size + 1); memset (data_s, 0, data_size); memcpy (data_s, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); } else { /* It's not a block algorithm */ data_size = Z_STRLEN_PP(data); data_s = emalloc(data_size + 1); memset(data_s, 0, data_size); memcpy(data_s, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); } mdecrypt_generic(pm->td, data_s, data_size); RETVAL_STRINGL(data_s, data_size, 1); efree(data_s);}/* }}} *//* {{{ proto array mcrypt_enc_get_supported_key_sizes(resource td) This function decrypts the crypttext */PHP_FUNCTION(mcrypt_enc_get_supported_key_sizes){ int argc, i, count; int *key_sizes; MCRYPT_GET_TD_ARG
argc = ZEND_NUM_ARGS(); key_sizes = mcrypt_enc_get_supported_key_sizes (pm->td, &count); if (array_init(return_value) == FAILURE) { php_error(E_ERROR, "%s(): Unable to initialize array", get_active_function_name(TSRMLS_C)); return; } if (count != 0) { for (i = 0; i < count; i++) { add_index_long(return_value, i, key_sizes[i]); } } mcrypt_free (key_sizes);}/* }}} *//* {{{ proto int mcrypt_enc_self_test(resource td) This function runs the self test on the algorithm specified by the descriptor td */PHP_FUNCTION(mcrypt_enc_self_test){ MCRYPT_GET_TD_ARG RETURN_LONG(mcrypt_enc_self_test(pm->td));}/* }}} *//* {{{ proto bool mcrypt_module_close(resource td) Free the descriptor td */PHP_FUNCTION(mcrypt_module_close){ MCRYPT_GET_TD_ARG zend_list_delete(Z_LVAL_PP(mcryptind)); RETURN_TRUE;}/* }}} *//* {{{ proto bool mcrypt_generic_end(resource td) This function terminates encrypt specified by the descriptor td */PHP_FUNCTION(mcrypt_generic_end){ MCRYPT_GET_TD_ARG#if HAVE_MCRYPT_GENERIC_DEINIT php_error(E_NOTICE, "%s(): This function is deprecated, please use mcrypt_generic_deinit()", get_active_function_name(TSRMLS_C)); if (mcrypt_generic_deinit(pm->td) < 0) {#else if (mcrypt_generic_end(pm->td) < 0) {#endif php_error (E_WARNING, "%s(): Could not terminate encryption specifier", get_active_function_name(TSRMLS_C)); RETURN_FALSE } RETURN_TRUE}/* }}} */#if HAVE_MCRYPT_GENERIC_DEINIT/* {{{ proto bool mcrypt_generic_deinit(resource td) This function terminates encrypt specified by the descriptor td */PHP_FUNCTION(mcrypt_generic_deinit){ MCRYPT_GET_TD_ARG if (mcrypt_generic_deinit(pm->td) < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not terminate encryption specifier"); RETURN_FALSE } RETURN_TRUE}/* }}} */#endif/* {{{ proto bool mcrypt_enc_is_block_algorithm_mode(resource td) Returns TRUE if the mode is for use with block algorithms */PHP_FUNCTION(mcrypt_enc_is_block_algorithm_mode){ MCRYPT_GET_TD_ARG if (mcrypt_enc_is_block_algorithm_mode(pm->td) == 1) { RETURN_TRUE } else { RETURN_FALSE }}/* }}} *//* {{{ proto bool mcrypt_enc_is_block_algorithm(resource td) Returns TRUE if the alrogithm is a block algorithms */PHP_FUNCTION(mcrypt_enc_is_block_algorithm){ MCRYPT_GET_TD_ARG if (mcrypt_enc_is_block_algorithm(pm->td) == 1) { RETURN_TRUE } else { RETURN_FALSE }}/* }}} *//* {{{ proto bool mcrypt_enc_is_block_mode(resource td) Returns TRUE if the mode outputs blocks */PHP_FUNCTION(mcrypt_enc_is_block_mode){ MCRYPT_GET_TD_ARG if (mcrypt_enc_is_block_mode(pm->td) == 1) { RETURN_TRUE } else { RETURN_FALSE }}/* }}} *//* {{{ proto int mcrypt_enc_get_block_size(resource td) Returns the block size of the cipher specified by the descriptor td */PHP_FUNCTION(mcrypt_enc_get_block_size){ MCRYPT_GET_TD_ARG RETURN_LONG(mcrypt_enc_get_block_size(pm->td));}/* }}} *//* {{{ proto int mcrypt_enc_get_key_size(resource td) Returns the maximum supported key size in bytes of the algorithm specified by the descriptor td */PHP_FUNCTION(mcrypt_enc_get_key_size){ MCRYPT_GET_TD_ARG RETURN_LONG(mcrypt_enc_get_key_size(pm->td));}/* }}} *//* {{{ proto int mcrypt_enc_get_iv_size(resource td) Returns the size of the IV in bytes of the algorithm specified by the descriptor td */PHP_FUNCTION(mcrypt_enc_get_iv_size){ MCRYPT_GET_TD_ARG RETURN_LONG(mcrypt_enc_get_iv_size(pm->td));}/* }}} *//* {{{ proto string mcrypt_enc_get_algorithms_name(resource td) Returns the name of the algorithm specified by the descriptor td */PHP_FUNCTION(mcrypt_enc_get_algorithms_name){ char *name; MCRYPT_GET_TD_ARG name = mcrypt_enc_get_algorithms_name(pm->td); RETVAL_STRING(name, 1); mcrypt_free(name);}/* }}} *//* {{{ proto string mcrypt_enc_get_modes_name(resource td) Returns the name of the mode specified by the descriptor td */PHP_FUNCTION(mcrypt_enc_get_modes_name){ char *name; MCRYPT_GET_TD_ARG name = mcrypt_enc_get_modes_name(pm->td); RETVAL_STRING(name, 1); mcrypt_free(name);}/* }}} *//* {{{ proto bool mcrypt_module_self_test(string algorithm [, string lib_dir]) Does a self test of the module "module" */PHP_FUNCTION(mcrypt_module_self_test){ MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir); if (mcrypt_module_self_test (module, dir) == 0) { RETURN_TRUE; } else { RETURN_FALSE; }}/* }}} *//* {{{ proto bool mcrypt_module_is_block_algorithm_mode(string mode [, string lib_dir]) Returns TRUE if the mode is for use with block algorithms */PHP_FUNCTION(mcrypt_module_is_block_algorithm_mode){ MCRYPT_GET_MODE_DIR_ARGS(modes_dir) if (mcrypt_module_is_block_algorithm_mode (module, dir) == 1) { RETURN_TRUE; } else { RETURN_FALSE; }}/* }}} *//* {{{ proto bool mcrypt_module_is_block_algorithm(string algorithm [, string lib_dir]) Returns TRUE if the algorithm is a block algorithm */PHP_FUNCTION(mcrypt_module_is_block_algorithm){ MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir) if (mcrypt_module_is_block_algorithm (module, dir) == 1) { RETURN_TRUE; } else { RETURN_FALSE; }}/* }}} *//* {{{ proto bool mcrypt_module_is_block_mode(string mode [, string lib_dir]) Returns TRUE if the mode outputs blocks of bytes */PHP_FUNCTION(mcrypt_module_is_block_mode){ MCRYPT_GET_MODE_DIR_ARGS(modes_dir) if (mcrypt_module_is_block_mode (module, dir) == 1) { RETURN_TRUE; } else { RETURN_FALSE; }}/* }}} *//* {{{ proto int mcrypt_module_get_algo_block_size(string algorithm [, string lib_dir]) Returns the block size of the algorithm */PHP_FUNCTION(mcrypt_module_get_algo_block_size){ MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir) RETURN_LONG(mcrypt_module_get_algo_block_size (module, dir));}/* }}} *//* {{{ proto int mcrypt_module_get_algo_key_size(string algorithm [, string lib_dir]) Returns the maximum supported key size of the algorithm */PHP_FUNCTION(mcrypt_module_get_algo_key_size){ MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir); RETURN_LONG(mcrypt_module_get_algo_key_size (module, dir));}/* }}} *//* {{{ proto array mcrypt_module_get_supported_key_sizes(string algorithm [, string lib_dir]) This function decrypts the crypttext */PHP_FUNCTION(mcrypt_module_get_supported_key_sizes){ int i, count; int *key_sizes; MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir) key_sizes = mcrypt_module_get_algo_supported_key_sizes (module, dir, &count); if (array_init(return_value) == FAILURE) { php_error(E_ERROR, "%s(): Unable to initialize array", get_active_function_name(TSRMLS_C)); return; } if (count != 0) { for (i = 0; i < count; i++) { add_index_long(return_value, i, key_sizes[i]); } } mcrypt_free (key_sizes);}/* }}} *//* {{{ proto array mcrypt_list_algorithms([string lib_dir]) List all algorithms in "module_dir" */PHP_FUNCTION(mcrypt_list_algorithms){ zval **lib_dir; char **modules; char *lib_dir_s; int i, count, argc; argc = ZEND_NUM_ARGS(); MCRYPT_CHECK_PARAM_COUNT (0,1) switch (argc) { case 1: if (zend_get_parameters_ex (1, &lib_dir) == FAILURE) { WRONG_PARAM_COUNT } convert_to_string_ex (lib_dir); lib_dir_s = Z_STRVAL_PP(lib_dir); break; case 0: lib_dir_s = INI_STR("mcrypt.algorithms_dir"); break; default: WRONG_PARAM_COUNT } modules = mcrypt_list_algorithms (lib_dir_s, &count); if (array_init(return_value) == FAILURE) { php_error(E_ERROR, "%s(): Unable to initialize array", get_active_function_name(TSRMLS_C)); return; } if (count == 0) { php_error (E_WARNING, "%s(): No algorithms found in module dir", get_active_function_name(TSRMLS_C)); } for (i = 0; i < count; i++) { add_index_string(return_value, i, modules[i], 1); } mcrypt_free_p (modules, count);}/* }}} *//* {{{ proto array mcrypt_list_modes([string lib_dir]) List all modes "module_dir" */PHP_FUNCTION(mcrypt_list_modes){ zval **lib_dir; char **modules; char *lib_dir_s; int i, count, argc; argc = ZEND_NUM_ARGS(); MCRYPT_CHECK_PARAM_COUNT (0,1) switch (argc) { case 1: if (zend_get_parameters_ex (1, &lib_dir) == FAILURE) { WRONG_PARAM_COUNT } convert_to_string_ex (lib_dir); lib_dir_s = Z_STRVAL_PP(lib_dir); break; case 0: lib_dir_s = MCG(modes_dir); break; default: WRONG_PARAM_COUNT } modules = mcrypt_list_modes (lib_dir_s, &count); if (array_init(return_value) == FAILURE) { php_error(E_ERROR, "%s(): Unable to initialize array", get_active_function_name(TSRMLS_C)); return; } if (count == 0) { php_error (E_WARNING, "%s(): No modes found in module dir", get_active_function_name(TSRMLS_C)); } for (i = 0; i < count; i++) { add_index_string(return_value, i, modules[i], 1); } mcrypt_free_p (modules, count);}/* }}} *//* {{{ proto int mcrypt_get_key_size(string cipher, string module) Get the key size of cipher */PHP_FUNCTION(mcrypt_get_key_size){ zval **cipher; zval **module; char *cipher_dir_string; char *module_dir_string; long key_size; MCRYPT td; MCRYPT_GET_INI if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &cipher, &module) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(cipher); convert_to_string_ex(module); td = mcrypt_module_open( Z_STRVAL_PP(cipher), cipher_dir_string, Z_STRVAL_PP(module), module_dir_string); if (td != MCRYPT_FAILED) { key_size = mcrypt_enc_get_key_size(td); mcrypt_module_close(td); RETVAL_LONG(key_size); } else { php_error (E_WARNING, MCRYPT_OPEN_MODULE_FAILED, get_active_function_name(TSRMLS_C)); RETVAL_LONG(0); }}/* }}} *//* {{{ proto int mcrypt_get_block_size(string cipher, string module) Get the key size of cipher */PHP_FUNCTION(mcrypt_get_block_size){ zval **cipher; zval **module; char *cipher_dir_string; char *module_dir_string; long key_size; MCRYPT td; MCRYPT_GET_INI if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &cipher, &module) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(cipher); convert_to_string_ex(module); td = mcrypt_module_open( Z_STRVAL_PP(cipher), cipher_dir_string, Z_STRVAL_PP(module), module_dir_string); if (td != MCRYPT_FAILED) { key_size = mcrypt_enc_get_block_size(td); mcrypt_module_close(td); RETVAL_LONG(key_size); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -