Skip to content

Instantly share code, notes, and snippets.

@galvao
Created December 26, 2011 16:57
Show Gist options
  • Save galvao/1521642 to your computer and use it in GitHub Desktop.
Save galvao/1521642 to your computer and use it in GitHub Desktop.
Usando mcrypt com Zend Framework via helpers
[production]
; Configuração normal do ZF
my.crypto.IV = "nononononononononononono"
my.crypto.Key = "nononononononononononononononono"
; A cifra e o modo devem ser CONSTANTES e não strings.
my.crypto.Cipher = MCRYPT_RIJNDAEL_192
my.crypto.Mode = MCRYPT_MODE_CFB
<?php
class Helper_Decrypt extends Zend_Controller_Action_Helper_Abstract
{
private $applicationConfig, $cryptoConfig, $result;
public function direct($cryptedData, $base64Decode = TRUE)
{
$this->applicationConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
$this->cryptoConfig = $this->applicationConfig->my->crypto;
if ($base64Decode) {
$cryptedData = base64_decode($cryptedData);
}
$this->result = mcrypt_decrypt($this->cryptoConfig->Cipher, $this->cryptoConfig->Key, $cryptedData, $this->cryptoConfig->Mode, $this->cryptoConfig->IV);
return $this->result;
}
}
?>
<?php
class Helper_Encrypt extends Zend_Controller_Action_Helper_Abstract
{
private $applicationConfig, $cryptoConfig, $result;
public function direct($rawData, $base64Encode = TRUE)
{
$this->applicationConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
$this->cryptoConfig = $this->applicationConfig->my->crypto;
$this->result = mcrypt_encrypt($this->cryptoConfig->Cipher, $this->cryptoConfig->Key, $rawData, $this->cryptoConfig->Mode, $this->cryptoConfig->IV);
if ($base64Encode) {
return base64_encode($this->result);
} else {
return $this->result;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment