Created
May 2, 2011 11:01
-
-
Save simukti/951443 to your computer and use it in GitHub Desktop.
Zend_Crypt_Rsa implant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* CORE | |
* Custom extended library for Zend Framework 1.10.x | |
* | |
* Copyright (c) 2011, Sarjono Mukti Aji <http://simukti.net/> | |
* All rights reserved. | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this libraries source code. | |
*/ | |
/** | |
* Rsa encryption wrapper with openssl private key | |
* | |
* @category Core | |
* @package Utiliyy | |
* @subpackage Rsa | |
* @author Sarjono Mukti Aji <http://simukti.net/> | |
* @copyright (c) 2011 - Sarjono Mukti Aji <http://simukti.net/> | |
*/ | |
class Core_Utility_Rsa | |
{ | |
/** | |
* SSL private key | |
* @var string | |
*/ | |
protected static $_privatekey; | |
/** | |
* Set ssl key path. I set it from application bootstrap. | |
* | |
* @param string $privatekey private key path | |
* @throws Core_Exception_InvalidArgumentException when private key was not set yet or file does not exists | |
*/ | |
public static function setPrivateKey($privatekey) | |
{ | |
if (null === $privatekey) { | |
throw new Core_Exception_UnexpectedValueException('Your private key was not set yet.'); | |
} | |
if (! file_exists($privatekey)) { | |
throw new Core_Exception_InvalidArgumentException(sprintf('Private key %s is not found.', $privatekey)); | |
} | |
self::$_privatekey = $privatekey; | |
} | |
/** | |
* Get ssl private key path | |
* | |
* @return string private key path. | |
*/ | |
public static function getPrivateKey() | |
{ | |
return self::$_privatekey; | |
} | |
/** | |
* Encrypt string | |
* | |
* @param string $string String to encrypt | |
* @return string Encrypted string | |
*/ | |
public static function encrypt($string) | |
{ | |
if (! is_string($string)) { | |
throw new Core_Exception_InvalidArgumentException('Param to encrypt is not a string.'); | |
} | |
$rsa = new Zend_Crypt_Rsa(array( | |
'pemPath' => self::getPrivateKey() | |
)); | |
$encrypted = $rsa->encrypt($string, $rsa->getPrivateKey(), Zend_Crypt_Rsa::BASE64); | |
return $encrypted; | |
} | |
/** | |
* Decrypt string | |
* | |
* @param string $string String to decrypt | |
* @return string Decrypted string | |
*/ | |
public static function decrypt($string) | |
{ | |
if (! is_string($string)) { | |
throw new Core_Exception_InvalidArgumentException('Param to decrypt is not a string.'); | |
} | |
$rsa = new Zend_Crypt_Rsa(array( | |
'pemPath' => self::getPrivateKey() | |
)); | |
$decrypted = $rsa->decrypt($string, $rsa->getPublicKey(), Zend_Crypt_Rsa::BASE64); | |
return $decrypted; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment