Beispiel #1
0
/**
 * Decrypt a text that is coded as a base64 string
 *
 * @param string $text
 * @param string $key
 * @return string
 */
PHP_METHOD(Phalcon_Crypt, decryptBase64){

	zval **text, **key = NULL, **safe = NULL, *decrypt_text;
	char *decoded;
	int decoded_len;

	phalcon_fetch_params_ex(1, 2, &text, &key, &safe);

	PHALCON_ENSURE_IS_STRING(text);
	if (!key) {
		key = &PHALCON_GLOBAL(z_null);
	}

	if (safe && zend_is_true(*safe)) {
		char *tmp = estrndup(Z_STRVAL_PP(text), Z_STRLEN_PP(text));
		php_strtr(tmp, Z_STRLEN_PP(text), "-_", "+/", 2);
		decoded = (char*)php_base64_decode((unsigned char*)tmp, Z_STRLEN_PP(text), &decoded_len);
		efree(tmp);
	}
	else {
		decoded = (char*)php_base64_decode((unsigned char*)(Z_STRVAL_PP(text)), Z_STRLEN_PP(text), &decoded_len);
	}

	if (!decoded) {
		RETURN_FALSE;
	}

	PHALCON_MM_GROW();
	PHALCON_ALLOC_GHOST_ZVAL(decrypt_text);
	ZVAL_STRINGL(decrypt_text, decoded, decoded_len, 0);
	PHALCON_RETURN_CALL_METHOD(this_ptr, "decrypt", decrypt_text, *key);
	RETURN_MM();
}
Beispiel #2
0
PHPAPI static int php_discuz_auth_decode(char *buf, const char *str, size_t str_len, const char *key, size_t keylen){
	char keya[33], keyb[33], keyc[DISCUZ_AUTH_CKEYLEN+1], cryptkey[65]={0};
	unsigned char *base64result;
	int retlen;
	time_t expiry;
	char md5salt[16], md5buf[33], *authcodebuf;
	smart_str strbuf = {0};

	memcpy(keyc, str, DISCUZ_AUTH_CKEYLEN);
	php_discuz_auth_initkey(keya, keyb, cryptkey, keyc, key, keylen);

	base64result = php_base64_decode((unsigned char*)(str + DISCUZ_AUTH_CKEYLEN), str_len-DISCUZ_AUTH_CKEYLEN, &retlen);

	authcodebuf = (unsigned char *)safe_emalloc(retlen, sizeof(char), 1);
	php_discuz_authcode((unsigned char *)authcodebuf, base64result, retlen, cryptkey);

	//expiry
	bzero(md5buf, sizeof(md5buf));
	memcpy(md5buf, authcodebuf, 10);
	expiry = atoi(md5buf);
	//md5
	memcpy(md5salt, authcodebuf + 10, 16);
	//if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
	smart_str_appendl(&strbuf, authcodebuf + 26, retlen - 26);
	smart_str_appendl(&strbuf, keyb, 32);
	
	php_md5(md5buf, strbuf.c, strbuf.len);
	smart_str_free(&strbuf);

	if((expiry == 0 || expiry - time(NULL) > 0) && memcmp(md5salt, md5buf, 16) == 0){
		memcpy(buf, authcodebuf + 26, retlen-26);
		efree(authcodebuf);
		return retlen-26;
	}
	efree(authcodebuf);
	return 0;	
}