Skip to content

Commit

Permalink
Merge pull request #1359 from dreamsxin/1353
Browse files Browse the repository at this point in the history
 Implement #1353 add `safe` param for \Phalcon\Crypt::*Base64
  • Loading branch information
Phalcon committed Oct 10, 2013
2 parents 45a32a5 + 661d4e2 commit 5442611
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
40 changes: 36 additions & 4 deletions ext/crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#include "kernel/string.h"
#include "kernel/concat.h"

#include "ext/standard/php_smart_str.h"
#include "ext/standard/php_string.h"

/**
* Phalcon\Crypt
*
Expand Down Expand Up @@ -566,19 +569,34 @@ PHP_METHOD(Phalcon_Crypt, decrypt){
*/
PHP_METHOD(Phalcon_Crypt, encryptBase64){

zval *text, *key = NULL, *encrypted;
zval *text, *key = NULL, *safe = NULL, *encrypted, *from, *to;

PHALCON_MM_GROW();

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

if (!key) {
key = PHALCON_GLOBAL(z_null);
}

if (!safe) {
safe = PHALCON_GLOBAL(z_false);
}

PHALCON_OBS_VAR(encrypted);
phalcon_call_method_p2_ex(encrypted, &encrypted, this_ptr, "encrypt", text, key);
phalcon_base64_encode(return_value, encrypted);

if (zend_is_true(safe)) {
PHALCON_INIT_VAR(from);
ZVAL_STRING(from, "+/", 1);

PHALCON_INIT_VAR(to);
ZVAL_STRING(to, "-_", 1);

php_strtr(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), Z_STRVAL_P(from), Z_STRVAL_P(to), MIN(Z_STRLEN_P(from), Z_STRLEN_P(to)));
}

RETURN_MM();
}

Expand All @@ -591,15 +609,29 @@ PHP_METHOD(Phalcon_Crypt, encryptBase64){
*/
PHP_METHOD(Phalcon_Crypt, decryptBase64){

zval *text, *key = NULL, *decrypt_text;
zval *text, *key = NULL, *safe = NULL, *decrypt_text, *from, *to;

PHALCON_MM_GROW();

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

if (!key) {
key = PHALCON_GLOBAL(z_null);
}

if (!safe) {
safe = PHALCON_GLOBAL(z_false);
}

if (zend_is_true(safe)) {
PHALCON_INIT_VAR(from);
ZVAL_STRING(from, "-_", 1);

PHALCON_INIT_VAR(to);
ZVAL_STRING(to, "+/", 1);

php_strtr(Z_STRVAL_P(text), Z_STRLEN_P(text), Z_STRVAL_P(from), Z_STRVAL_P(to), MIN(Z_STRLEN_P(from), Z_STRLEN_P(to)));
}

PHALCON_INIT_VAR(decrypt_text);
phalcon_base64_decode(decrypt_text, text);
Expand Down
2 changes: 2 additions & 0 deletions ext/crypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_crypt_encryptbase64, 0, 0, 1)
ZEND_ARG_INFO(0, text)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, safe)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_crypt_decryptbase64, 0, 0, 1)
ZEND_ARG_INFO(0, text)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, safe)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_crypt_getpadding, 0, 0, 0)
Expand Down
17 changes: 17 additions & 0 deletions unit-tests/CryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,21 @@ public function testPadding()
}
}
}

public function testEncryptBase64()
{
$crypt = new \Phalcon\Crypt();
$crypt->setPadding(\Phalcon\Crypt::PADDING_ANSI_X_923);

$key = 'phalcon';
$text = 'https://github.com/phalcon/cphalcon/issues?state=open';

$encrypted = $crypt->encryptBase64($text, $key);
$actual = $crypt->decryptBase64($encrypted, $key);
$this->assertEquals($actual, $text);

$encrypted = $crypt->encryptBase64($text, $key, TRUE);
$actual = $crypt->decryptBase64($encrypted, $key, TRUE);
$this->assertEquals($actual, $text);
}
}

0 comments on commit 5442611

Please sign in to comment.