Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Hotfix/random crypt test fail #3492

Merged
merged 2 commits into from
Jan 23, 2013
Merged

Hotfix/random crypt test fail #3492

merged 2 commits into from
Jan 23, 2013

Conversation

Maks3w
Copy link
Member

@Maks3w Maks3w commented Jan 19, 2013

The problem is that base64_decode strict check is not enough for ensure base64 encoding detection.

See http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php

@ghost ghost assigned ezimuel Jan 21, 2013
@ezimuel
Copy link
Contributor

ezimuel commented Jan 21, 2013

@Maks3w see my comment above.

@ezimuel
Copy link
Contributor

ezimuel commented Jan 22, 2013

The correct way to check for base64 is to apply my fix, the check with "strlen() % 4" is not enough.
I tried on my box (GNU/Linux 3.2.0-36, PHP 5.3.10, PHPUnit 3.7) with your fix and I got a random error after 70/90 executions.
Using my proposal the fix disappears.
Please, update your PR with my suggestion and I will merge the fix.

@Maks3w
Copy link
Member Author

Maks3w commented Jan 22, 2013

@ezimuel Then I think that we need a different aproach. Decode + Encode sounds a waste of resources.

I suggest to have two methods one for base64 encode and another one without base64 de encode or to have an argument which specify if the input is or not base64

@ezimuel
Copy link
Contributor

ezimuel commented Jan 22, 2013

I don't think that this approach is a waste of resource. I only added a base64_encode() in the code. Moreover, the usage of the decrypt() or encrypt() method in the Rsa class is only for small strings, see http://zf2.readthedocs.org/en/latest/modules/zend.crypt.public-key.html#encrypt-and-decrypt-a-string for more details.

@marc-mabe
Copy link
Member

From my point of view the method decrypt can't autodetect if $data is encoded in base64 because:

  • There are more than one variants of base64
  • base64 with or without the leading = can be valid
  • I don't see a definition that encrypted $data have to contain a defined set of bytes that can't be interpreted as base64 in all cases

That means it's unknown and should be up to the consumer to know how $data is encoded and having a defined API working with binary data so the consumer have to decode it or defining different methods handle well defined types of encoded data.

(Sorry for my poor English)

@ezimuel
Copy link
Contributor

ezimuel commented Jan 22, 2013

We have discussed with @Maks3w and we decided to propose an alternative signature for the decrypt method.
Something like:

public function decrypt ($data, $key = null, $mode = self::MODE_AUTO)

where $mode can be MODE_AUTO with the automatic recognition of base64 (with my suggested change), MODE_BASE64 with the decode of the $data in base64 and MODE_RAW without any decoding.
We think this API will cover all the possible use case taking care of performance. At the end, as @marc-mabe suggested the user should now the format for decrypt a string.

@Maks3w Maks3w closed this Jan 22, 2013
@Maks3w Maks3w reopened this Jan 23, 2013
@Maks3w
Copy link
Member Author

Maks3w commented Jan 23, 2013

@ezimuel done

if (false !== $output) {
$data = $output;
switch ($mode) {
case self::MODE_AUTO:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we deprecate this mode and trigger an E_USER_DEPRECATED error ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a feature.

@Maks3w
Copy link
Member Author

Maks3w commented Jan 23, 2013

done

@ezimuel ezimuel merged commit 44265c3 into zendframework:master Jan 23, 2013
@Maks3w Maks3w deleted the hotfix/random-crypt-test-fail branch January 23, 2013 17:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants