RSA: make RSA objects clonable

This commit is contained in:
terrafrost 2013-10-18 13:24:06 -05:00
parent 22502c24cd
commit 1e3ff81e7d
2 changed files with 82 additions and 1 deletions

View File

@ -82,6 +82,15 @@ define('CRYPT_HASH_MODE_HASH', 3);
* @package Crypt_Hash * @package Crypt_Hash
*/ */
class Crypt_Hash { class Crypt_Hash {
/**
* Hash Parameter
*
* @see Crypt_Hash::setHash()
* @var Integer
* @access private
*/
var $hashParam;
/** /**
* Byte-length of compression blocks / key (Internal HMAC) * Byte-length of compression blocks / key (Internal HMAC)
* *
@ -174,6 +183,19 @@ class Crypt_Hash {
$this->key = $key; $this->key = $key;
} }
/**
* Gets the hash function.
*
* As set by the constructor or by the setHash() method.
*
* @access public
* @return String
*/
function getHash()
{
return $this->hashParam;
}
/** /**
* Sets the hash function. * Sets the hash function.
* *
@ -182,7 +204,7 @@ class Crypt_Hash {
*/ */
function setHash($hash) function setHash($hash)
{ {
$hash = strtolower($hash); $this->hashParam = $hash = strtolower($hash);
switch ($hash) { switch ($hash) {
case 'md5-96': case 'md5-96':
case 'sha1-96': case 'sha1-96':

View File

@ -1393,6 +1393,53 @@ class Crypt_RSA {
*/ */
function loadKey($key, $type = false) function loadKey($key, $type = false)
{ {
if (is_object($key) && strtolower(get_class($key)) == 'crypt_rsa') {
$this->privateKeyFormat = $key->privateKeyFormat;
$this->publicKeyFormat = $key->publicKeyFormat;
$this->k = $key->k;
$this->hLen = $key->hLen;
$this->sLen = $key->sLen;
$this->mgfHLen = $key->mgfHLen;
$this->encryptionMode = $key->encryptionMode;
$this->signatureMode = $key->signatureMode;
$this->password = $key->password;
$this->configFile = $key->configFile;
$this->comment = $key->comment;
if (is_object($key->hash)) {
$this->hash = new Crypt_Hash($key->hash->getHash());
}
if (is_object($key->mgfHash)) {
$this->mgfHash = new Crypt_Hash($key->mgfHash->getHash());
}
if (is_object($key->modulus)) {
$this->modulus = $key->modulus->copy();
}
if (is_object($key->exponent)) {
$this->exponent = $key->exponent->copy();
}
if (is_object($key->publicExponent)) {
$this->publicExponent = $key->publicExponent->copy();
}
$this->primes = array();
$this->exponents = array();
$this->coefficients = array();
foreach ($this->primes as $prime) {
$this->primes[] = $prime->copy();
}
foreach ($this->exponents as $exponent) {
$this->exponents[] = $exponent->copy();
}
foreach ($this->coefficients as $coefficient) {
$this->coefficients[] = $coefficient->copy();
}
return true;
}
if ($type === false) { if ($type === false) {
$types = array( $types = array(
CRYPT_RSA_PUBLIC_FORMAT_RAW, CRYPT_RSA_PUBLIC_FORMAT_RAW,
@ -1603,6 +1650,18 @@ class Crypt_RSA {
return $key !== false ? $key : ''; return $key !== false ? $key : '';
} }
/**
* __clone() magic method
*
* @access public
*/
function __clone()
{
$key = new Crypt_RSA();
$key->loadKey($this);
return $key;
}
/** /**
* Generates the smallest and largest numbers requiring $bits bits * Generates the smallest and largest numbers requiring $bits bits
* *